1

我的目标是通过 android 设备检索 JSON。我用来成功的代码如下:

 public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();           

    }

不幸的是,因为我无法成功连接到内部人员,我在 HttpResponse 行收到以下错误:

java.lang.runtimeexception 无法启动活动组件信息:android.os.Network

我在 Stackoverflow 的其他线程中读到,可能的解决方案是启动一个新线程。但我不知道该怎么做。

PS 我已经在 androidmanifest.xml 中声明了互联网权限行,即

<uses-permission android:name="android.permission.INTERNET" />
4

2 回答 2

0

尝试这个..

public JSONObject getJSONFromUrl(String url) {
                 JSONObject j = null;
        // Making HTTP request
        try {
            // defaultHttpClient
            HttpParams httpParameters = new BasicHttpParams();
            HttpClient httpClient = new DefaultHttpClient(httpParameters);
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            //HttpEntity httpEntity = httpResponse.getEntity();
           // is = httpEntity.getContent(); 
            j = new JSONObject(EntityUtils.toString(httpResponse.getEntity()));

        }finally{
                     return j;
                }
}
于 2012-10-05T20:26:02.667 回答
0

这是我编写的一个类,用于从服务器(谷歌应用引擎)查询数据,它建立连接并在线读取内容。

public class QueryServer 
{ 
    public static final String URL = "http://10.0.2.2:8888/";

@SuppressWarnings("unused")
private class GetXMLTask extends AsyncTask<String, Void, String>
{
    @Override
    protected String doInBackground(String... urls)
    {           
        String output = null;           
        try
        {               
             for (String url : urls) 
             {                      
                output = getOutputFromUrl(url); 
                Log.d("Server Return", output);
             }               
        }
        catch(Exception e)
        {
            Log.d("Exception", e.getMessage());
        }
        return output;
    }           
  } 
  private String getOutputFromUrl(String url)
  {
        StringBuffer output = new StringBuffer("");
        try 
        {
            InputStream stream = getHttpConnection(url);
            BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));
            String s = "";
            while ((s = buffer.readLine()) != null)
            {
                output.append(s);
                output.toString();
            }
        }
        catch (IOException e1) 
        {
            e1.printStackTrace();
        }

        return output.toString();
    } 
    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString) throws IOException 
    {
        InputStream stream = null;
        try 
        {                
            java.net.URL url = new java.net.URL(urlString);
            URLConnection connection = url.openConnection();      
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) 
            {
                stream = httpConnection.getInputStream();
            }
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
        return stream;
    } 
    protected void onPostExecute(String output) 
    {

    }
}
于 2012-10-05T20:16:22.817 回答