【问题标题】:FileNotFoundException while parsing json in android 4.0.4 but works in android 2.3.3FileNotFoundException 在 android 4.0.4 中解析 json 但适用于 android 2.3.3
【发布时间】:2013-01-11 19:18:44
【问题描述】:

我正在尝试访问显示我的 android 应用程序内容的 json。 FileNotFound 异常在 Android 4.0.4 中抛出,但 json 可以在 Android 2.3.3 中访问。也可以使用相同的 url 从浏览器访问 json。代码有什么问题?任何人都可以帮助我解决这个问题。

我正在使用下面的代码:

url = new URL("http://www.dynamiskdesign.se/ipromotionnew/json/148.json");

HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();

InputStream input = httpURLConnection.getInputStream();

if (input != null) {
    writer = new StringWriter();
    char[] buffer = new char[1024];
    Reader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
    int n;
    while ((n = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, n);
    }
    input.close();
}

String jsontext = writer.toString();

【问题讨论】:

  • 在哪一行抛出异常? logcat 中有更多信息吗?
  • @RvdK 这里不需要 Logcat。这是因为:此错误来自 HoneyComb(3.0 或更高版本)。如文档所述,您无法在其主线程上执行网络操作。要解决这个问题,您必须使用处理程序或异步任务。

标签: java android http


【解决方案1】:

这是因为网络和线程的结合。
该页面上也有说明(链接)。 我也有这个,看看这个答案: httpclient (phpmyadmin) not working on Android 4.0+

我认为这个解决方案可以工作:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
//  
public class Connector extends AsyncTask<TextView, Void, String> {

    @Override
    protected String doInBackground(TextView... params) {
        // TODO Auto-generated method stub
        return GetSomething();
    }

    private final String getSomething() {   
         try{
                url=new URL("http://www.dynamiskdesign.se/ipromotionnew/json/148.json");
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.connect();
                InputStream input=httpURLConnection.getInputStream();

        } catch(Exception e) {
                Log.e("log_tag", "Error in http connection " + e.toString());
        }

        //convert response to string
        try{
                if (input != null) 
                {
                    writer = new StringWriter();
                    char[] buffer = new char[1024];
                    Reader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
                    input.close();
                }

        } catch(Exception e) {
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        //parse json data
        try {
                String jsontext = writer.toString();

        } catch(JSONException e) {
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return returnString; 
    }    

    protected void onPostExecute(String page) {   
        //onPostExecute
    }   
}

【讨论】:

  • 您提供的链接没有说明有关 FileNotFoundException 的任何内容。你确定这是相关的吗?
  • @RvdK 这是关于我自己的经历。因为网络在 3.0+ 上无法正常工作,所以他找不到 File。我得到一个NullPointer,因为它在 3.0+ 上找不到String。这里的问题是一样的,但在这种情况下是File,而不是String。但是那个家伙还没有回复:p
  • @Bigflow 非常感谢 :) 我在这里使用了异步任务,效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
相关资源
最近更新 更多