Android,PHP,HttpGet

时间:2015-04-30 22:29:04

标签: php android json

我目前有一个我编写的php文件的URL,它生成以下内容(我相信JSON字符串)

{"id":"10","email":"test@uconn.edu","username":"test","bio":"I am not real","karma":"200","posted_jobs":"Posted job","accepted_jobs":"Accepted jobs"}

我需要在android studio中获取这个...但是我试图在很多方面这样做,但无论应用程序在我发出http请求时崩溃(从我发现的许多默认JSONParser类之一)。我也尝试了不同的解析器方法但没有工作。)

我应该使用特殊格式检索吗?老实说,我从未花费过多少时间处理一个问题。变得非常沮丧> :(

编辑: 我尝试使用的方法之一

public JSONObject makeHttpRequest(String url){
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {
        HttpResponse httpresponse = httpclient.execute(httppost);
        HttpEntity httpentity = httpresponse.getEntity();
        is = httpentity.getContent();

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while((line = reader.readLine())!=null){
                sb.append(line+"\n");

            }
            is.close();
            json = sb.toString();
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return jObj;

}

编辑:调试日志结果

04-30 19:11:33.670  32306-32306/uconn.campusoddjobs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: uconn.campusoddjobs, PID: 32306
android.os.NetworkOnMainThreadException
        at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1239)
        at java.net.InetAddress.lookupHostByName(InetAddress.java:388)
        at java.net.InetAddress.getAllByNameImpl(InetAddress.java:239)
        at java.net.InetAddress.getAllByName(InetAddress.java:214)
        at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
        at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:367)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:519)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:497)
        at uconn.campusoddjobs.JSONparser.getJSONFromUrl(JSONparser.java:48)
        at uconn.campusoddjobs.Profile.<init>(Profile.java:39)
        at uconn.campusoddjobs.MyAccountFragment.onCreateView(MyAccountFragment.java:23)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5633)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您正在主线程上发出请求。 不得在主线程上进行网络操作,因为它们会阻止您的用户界面。

您可以按顺序使用ASyncTask(http://developer.android.com/reference/android/os/AsyncTask.html)。

这是一个教程:http://mobiledevtuts.com/android/android-http-with-asynctask-example/