从Android的HTTP请求读取结果 [英] Reading Result from HTTP Request in Android

查看:246
本文介绍了从Android的HTTP请求读取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要阅读连接到一个在线的PHP脚本后检索的响应消息。该脚本返回如下:

  {成功:1,innerResult:[{用户名:拉法特,密码:123}]}

我只需要阅读这条消息的成功价值。我尝试HTTP请求:

 私有类GetPersonDetails扩展的AsyncTask<字符串,字符串,字符串> {
        //活动的行为;
        上下文mContext;        ProgressDialog progressDialog;        @覆盖
        / *
        在preExecute公共无效(){            progressDialog =新ProgressDialog(mContext);
            progressDialog.setMessage(登录...);
            progressDialog.show();
        }
* /        保护字符串doInBackground(字符串... PARAMS){
            //从后台线程更新界面            //检查成功标记            尝试{
                成功= 3;
                清单<&的NameValuePair GT; paramss =新的ArrayList<&的NameValuePair GT;();
                成功= 4;
                paramss.add(新BasicNameValuePair(用户名,n.getText()的toString()));
                paramss.add(新BasicNameValuePair(密码,p.getText()的toString()));
                成功= 5;
                JSONObject的JSON = jsonParser.makeHtt prequest(akmiengineering.com/insurance-app/search_username.php,GET,paramss);
                成功= 6;                // JSON的成功标签
                成功= json.getInt(成功);
                    Toast.makeText(getApplicationContext(),成功是+成功,Toast.LENGTH_LONG).show();                如果(成功== 1){
                    // onPostExecute();
.......        }其他{.......
                    }
                }赶上(例外五){
                    // Toast.makeText(getApplicationContext(),错误,Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }                返回null;
            }

在code止于此:

  JSONObject的JSON = jsonParser.makeHtt prequest(akmiengineering.com/insurance-app/search_username.php,GET,paramss);

和成功值为5。我无法检测到什么导致了问题,所以我尝试使用URL连接,而不是:

  URL网址;
        HttpURLConnection类的URLConnection = NULL;
        尝试{
            URL =新的URL(http://akmiengineering.com/insurance-app/search_username.php?username=adla&password=123);            URLConnection的=(HttpURLConnection类)url.openConnection();            在的InputStream = urlConnection.getInputStream();            InputStreamReader的ISW =新的InputStreamReader(中);            int数据= isw.read();
            而(数据!= -1){
                烧焦电流=(char)的数据;
                数据= isw.read();
                System.out.print(电流);
            }
        }赶上(例外五){
            e.printStackTrace();
        } {最后
            尝试{
                urlConnection.disconnect();
            }赶上(例外五){
                e.printStackTrace(); //如果你想在失败的进一步信息...
            }

也是我的code是停在这儿:

 中的InputStream = urlConnection.getInputStream();

我不知道它是什么,我做错了。请帮我。

更新:

这是logcat的:

  14 07  -  09:03:58.221 9554-9610 / system_process E / InputDispatcher:运动事件具有无效的指针数0;值必须在1和16之间。
07-09 14:03:58.321 9554-9610 / system_process E / InputDispatcher:运动事件具有无效的指针数0;值必须在1和16之间。
07-09 14:03:58.321 598-1749 /? D / audio_hw_primary:找到了的/ dev / SND / pcmC0D0p
07-09 14:03:58.381 598-1749 /? W / audio_hw_primary:out_write()限制睡眠时间为102539 46439
07-09 14:03:58.451 598-1749 /? W / audio_hw_primary:out_write()限制睡眠时间65759至46439
07-09 14:04:03.831 9750-10534 / com.bluestacks.bstfolder D / dalvikvm:GC_FOR_ALLOC释放711K,25%的自由2688K / 3576K,暂停0毫秒,共0毫秒
07-09 14:04:09.951 9783-9911 / com.bluestacks.gamepophome D / [的InMobi] - [分析] -4.5.3:NetworkManager-> handleMessag:消息:{=时候什么-40ms = 1001 TARGET =机器人.os.Handler}
07-09 14:04:18.291 591-1700 /? D / MDnsDS:MDnsSdListener ::监视调查超时
07-09 14:04:18.291 591-1700 /? D / MDnsDS:要与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的:在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.net.InetAddress.getAllByName(InetAddress.java:214)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在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的:在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的:在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的:在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的:在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的:在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的:在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的:在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的:在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的:在com.example.adla.insurancemobileapplication.MainActivity.yalaconnect(MainActivity.java:187)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.lang.reflect.Method.invokeNative(本机方法)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.lang.reflect.Method.invoke(Method.java:515)
07-09 14:04:19.591 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.view.View $ 1.onClick(View.java:3823)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.view.View.performClick(View.java:4443)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.view.View $ PerformClick.run(View.java:18433)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.os.Handler.handleCallback(Handler.java:733)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.os.Handler.dispatchMessage(Handler.java:95)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.os.Looper.loop(Looper.java:136)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在android.app.ActivityThread.main(ActivityThread.java:5021)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.lang.reflect.Method.invokeNative(本机方法)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在java.lang.reflect.Method.invoke(Method.java:515)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在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的:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication W / System.err的:在dalvik.system.NativeStart.main(本机方法)
07-09 14:04:19.601 12243-12243 / com.example.adla.insurancemobileapplication I /编舞:跳过1275帧!该应用程序可能会做它的主线程的工作太多了。
07-09 14:04:58.951 9750-10534 / com.bluestacks.bstfolder D / dalvikvm:GC_FOR_ALLOC释放729K,25%的自由2687K / 3576K,暂停0毫秒,共0毫秒


解决方案

尝试创建一个新的项目只是为了测试你的code的URL连接,因为我测试了code和其工作正常。

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.

这篇关于从Android的HTTP请求读取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆