简体   繁体   English

在Android 4.0.4中解析json时出现FileNotFoundException,但在Android 2.3.3中有效

[英]FileNotFoundException while parsing json in android 4.0.4 but works in android 2.3.3

I am trying to access the json that displays the contents of my android application. 我正在尝试访问显示android应用程序内容的json。 The FileNotFound exception is throwing in Android 4.0.4 but the json can be accessed in Android 2.3.3. FileNotFound异常在Android 4.0.4中引发,但可以在Android 2.3.3中访问json。 The json can also be accessed from browser using the same url. 还可以使用相同的URL从浏览器访问json。 What is wrong in the code ? 代码有什么问题? Can anyone help me regarding this issue. 谁能帮我解决这个问题。

I am using the code below: 我正在使用以下代码:

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();

This is because the combination of network and threads. 这是因为网络和线程的组合。
Explanation is also on that page (link). 该页面(链接)上也有说明。 I had this too, look at this answer: httpclient (phpmyadmin) not working on Android 4.0+ 我也有这个,看看这个答案: httpclient(phpmyadmin)在Android 4.0+上不起作用

I think this solution can work: 我认为这种解决方案可以工作:

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
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM