Issue
I am trying to get json data from a table that i am querying in my wamp server with a select.php the data shows in the browser but android for some reason can't download the data.
select.php
<?php
$conn=mysqli_connect("localhost","root","", "shady") or die("Unable to connect");
if(mysqli_connect_error($conn)) {
echo "Failed To Connect";
}
$qry = "SELECT * FROM `wellden`";
$res = mysqli_query($conn, $qry);
if ($res) {
while ($row = mysqli_fetch_array($res)) {
$flag[] = $row;
}
// returns tthe json data
echo json_encode($flag);
}
mysqli_close($conn);
?>
works fine and prints data in browser:
but for some weird reason android cant seem to get the echoed data from the php file.
Android code:
public class ListOfCourses extends AppCompatActivity {
ListView listView;
ArrayList<String> arrayList;
ArrayAdapter<String> arrayAdapter;
String REG_URL;
public class Listdata extends AsyncTask<String, Void, String>{
HttpURLConnection conn;
URL url;
String jsonStringForMe;
@Override
protected String doInBackground(String... strings) {
// MOST CRITICAL PART.....................................
String s = strings[0];
try {
url = new URL(s);
conn = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
// Append server response in string
sb.append(line);
}
// Stroring it to use in the onPostExecute Cause we do not have any Button in this view
jsonStringForMe = sb.toString();
return jsonStringForMe;
}catch (Exception e){
return e.getMessage();
}
// MOST CRITICA PART ENDS HERE..............................
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// JsonParsing Happens Here
Toast.makeText(getApplicationContext(), jsonStringForMe+" ", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_courses);
Listdata listdata = new Listdata();
REG_URL="http://1.0.0.2/android_db_pool/select.php";
listdata.execute(REG_URL);
}
}
but everytime the jsonStringForMe gives me null in postExecute everything seems Ok should work, but for some reason does not work. What can be the cause of the json data not receiving in the android end ?
EDIT I added internet permission also:
AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.slimshady.jamalsir1">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ListOfCourses"></activity>
</application>
</manifest>
EDIT 2
It may seem that ip address may be an issue but it is not insertion works fine with insert.php, and i can get the json data with the ip address that is my server's ip address 1.0.0.2. In android i am using the ip address of the server not localhost in only php i am using localhost because the php file is in my server.
Solution
As already commented;
Stroring it to use in the
onPostExecute
Cause we do not have any Button in this view
You didn't add or use to get the json in onPostExecute()
method but you have added the Toast
in onPreExecute
.
Try to add onPostExecute()
then show Toast
.
protected void
onPreExecute()
Runs on the UI thread before
doInBackground(Params...)
.
https://developer.android.com/reference/android/os/AsyncTask#onPreExecute%28%29
Answered By - ʍѳђઽ૯ท
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.