Android异常与互联网连接有关

时间:2012-10-05 19:25:06

标签: android json

我的目标是通过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();           

    }

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

java.lang.runtimeexception无法启动活动componentinfo:android.os.Network

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

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

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

2 个答案:

答案 0 :(得分:0)

这是我用来查询来自服务器(Google应用引擎)的数据的一个类,它建立了连接并在线读取内容。

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) 
    {

    }
}

答案 1 :(得分: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;
                }
}