我需要阅读连接到在线PHP脚本后检索到的响应消息。此脚本返回以下内容:
{"success":1,"innerResult":[{"username":"raafat","password":"123"}]}
我只需要从此消息中读取成功值。我尝试了HTTP请求:
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;
}
代码在此处停止:
JSONObject json = jsonParser.makeHttpRequest("akmiengineering.com/insurance-app/search_username.php", "GET", paramss);
并且成功值为5.我无法检测到导致问题的原因,所以我尝试使用URL连接:
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...
}
但我的代码也停在这里:
InputStream in = urlConnection.getInputStream();
我不知道我做错了什么。请帮帮我。
更新:
这是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
答案 0 :(得分:1)
尝试创建一个新项目只是为了测试代码的url连接,因为我测试了你的代码并且它正在运行。
答案 1 :(得分:0)
网址似乎没问题。您可能需要设置HTTP代理才能连接。如果您正在设备上进行调试而其他一切正常,那就很奇怪了。如果您使用的是模拟器,请尝试按照this question的答案更改设置:
--http-proxy
参数
makeHttpRequest
方法支持这种方式,但您可以java.net.Proxy
使用HttpURLConnection
,如上所述here < / LI>
答案 2 :(得分:0)