gpt4 book ai didi

php - 在 Android 中从 HTTP 请求读取结果

转载 作者:行者123 更新时间:2023-11-30 02:01:59 24 4
gpt4 key购买 nike

我需要读取连接到在线 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();

我不知道我做错了什么。请帮助我。

更新:

这是日志:

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

最佳答案

尝试创建一个新项目来测试您的 url 连接代码,因为我测试了您的代码并且它正在运行。

关于php - 在 Android 中从 HTTP 请求读取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31313236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com