使用HttpURLConnection从android 6.0中的本地服务器获取JSON数组

时间:2017-11-19 08:44:29

标签: android json httpurlconnection

我试图从这个本地服务器获取一个JSON数组五天:

localhost/match_picture/service.php?action=read

我无法做到!!

我在谷歌搜索并阅读了太多文档!

这是我的代码:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebService {

    public static String readUrl(String server_url) {
                 BufferedReader bufferedReader = null;
                    try {
                        URL url = new URL(server_url);
                        HttpURLConnection con = (HttpURLConnection) url.openConnection();
                        StringBuilder sb = new StringBuilder();
                        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                        String json;
                        while ((json = bufferedReader.readLine()) != null) {
                        sb.append(json+"\n");

                    }
                    return sb.toString();

                }catch (Exception e) {
                    e.printStackTrace();
                }

                return null;

            }
    }

它是Main_Activity:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;


public class Activity_main extends AppCompatActivity {

    private ArrayList<StructAcount> netAcount = new ArrayList<StructAcount>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String result= WebService.readUrl("http://localhast/match_picture/service.php?action=read");
        if (result != null) {
            try {
                JSONArray tasks = new JSONArray(result);
                for (int i=0; i<tasks.length(); i++) {
                    StructAcount acount= new StructAcount();
                    JSONObject object = tasks.getJSONObject(i);
                    acount.id = object.getLong("user_id");
                    acount.name = object.getString("user_name");
                    acount.email = object.getString("user_email");

                    netAcount.add(acount);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        for (StructAcount acount: netAcount) {
            Toast.makeText(Activity_main.this, "username: " + acount.name + "\n" + "useremail: " + acount.email , Toast.LENGTH_SHORT).show();
        }
    }

}

它在模拟器上运行并在此行中崩溃:

bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

我不知道为什么......

我正在搜索五天!!!!

我可以用HttpClient来做 但我想要更新

我在youtube中看到一个在Main_Activity中创建一个类的vidoe扩展了AsyncTask并在doInBackground(String ... params)中进行连接。我尝试了,这是正确的。但是因为我想在anoder类(WebService)中执行它而我不知道如何将结果发送到Main_Activity,我删除了从AsyncTask扩展的类。

谢谢你的帮助 抱歉我的英语不好

1 个答案:

答案 0 :(得分:0)

您开始使用NetworkOnMainThreadException

你的应用程序崩溃。

谷歌如何解决它。