I need to read the response message retrieved after connecting to an online PHP script. This script returns the following:
{"success":1,"innerResult":[{"username":"raafat","password":"123"}]}
I just need to read the success value from this message. I tried HTTP Request:
private class GetPersonDetails extends AsyncTask<String, String, String> {
//Activity act;
Context mContext;
ProgressDialog progressDialog;
@Override
/*
public void onPreExecute() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Signing in...");
progressDialog.show();
}
*/
protected String doInBackground(String... params) {
// updating UI from Background Thread
// Check for success tag
try {
success = 3;
List<NameValuePair> paramss = new ArrayList<NameValuePair>();
success = 4;
paramss.add(new BasicNameValuePair("username",n.getText().toString()));
paramss.add(new BasicNameValuePair("password",p.getText().toString()));
success = 5;
JSONObject json = jsonParser.makeHttpRequest("akmiengineering.com/insurance-app/search_username.php", "GET", paramss);
success = 6;
// json success tag
success = json.getInt("success");
Toast.makeText(getApplicationContext(),"success is" + success, Toast.LENGTH_LONG).show();
if (success == 1) {
// onPostExecute();
.......
} else {
.......
}
} catch (Exception e) {
// Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return null;
}
The code stops here:
JSONObject json = jsonParser.makeHttpRequest("akmiengineering.com/insurance-app/search_username.php", "GET", paramss);
and the success value is 5. I couldn't detect what's causing the problem, so I tried using URL Connection instead:
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://akmiengineering.com/insurance-app/search_username.php?username=adla&password=123");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace(); //If you want further info on failure...
}
But also my code is stopping here:
InputStream in = urlConnection.getInputStream();
I don't know what is it that I'm doing wrong. Please help me.
UPDATE:
This is the logcat:
07-09 14:03:58.221 9554-9610/system_process E/InputDispatcher﹕ Motion event has invalid pointer count 0; value must be between 1 and 16.
07-09 14:03:58.321 9554-9610/system_process E/InputDispatcher﹕ Motion event has invalid pointer count 0; value must be between 1 and 16.
07-09 14:03:58.321 598-1749/? D/audio_hw_primary﹕ found out /dev/snd/pcmC0D0p
07-09 14:03:58.381 598-1749/? W/audio_hw_primary﹕ out_write() limiting sleep time 102539 to 46439
07-09 14:03:58.451 598-1749/? W/audio_hw_primary﹕ out_write() limiting sleep time 65759 to 46439
07-09 14:04:03.831 9750-10534/com.bluestacks.bstfolder D/dalvikvm﹕ GC_FOR_ALLOC freed 711K, 25% free 2688K/3576K, paused 0ms, total 0ms
07-09 14:04:09.951 9783-9911/com.bluestacks.gamepophome D/[InMobi]-[Analytics]-4.5.3﹕ NetworkManager->handleMessag: msg:{ when=-40ms what=1001 target=android.os.Handler }
07-09 14:04:18.291 591-1700/? D/MDnsDS﹕ MDnsSdListener::Monitor poll timed out
07-09 14:04:18.291 591-1700/? D/MDnsDS﹕ Going to poll with pollCount 1
07-09 14:04:19.581 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ android.os.NetworkOnMainThreadException
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.example.adla.insurancemobileapplication.MainActivity.yalaconnect(MainActivity.java:187)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-09 14:04:19.591 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.view.View$1.onClick(View.java:3823)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.view.View.performClick(View.java:4443)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.view.View$PerformClick.run(View.java:18433)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5021)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-09 14:04:19.601 12243-12243/com.example.adla.insurancemobileapplication I/Choreographer﹕ Skipped 1275 frames! The application may be doing too much work on its main thread.
07-09 14:04:58.951 9750-10534/com.bluestacks.bstfolder D/dalvikvm﹕ GC_FOR_ALLOC freed 729K, 25% free 2687K/3576K, paused 0ms, total 0ms
Try creating a new project just to test your code for url connection, because I tested your code and it is working.