Java 在android 4.0.4中解析json时出现FileNotFoundException,但在android 2.3.3中有效
我正在尝试访问显示android应用程序内容的json。FileNotFound异常在Android 4.0.4中抛出,但json可以在Android 2.3.3中访问。也可以使用相同的url从浏览器访问json。代码中有什么错误?有人能帮我解决这个问题吗 我使用的代码如下:Java 在android 4.0.4中解析json时出现FileNotFoundException,但在android 2.3.3中有效,java,android,http,Java,Android,Http,我正在尝试访问显示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
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();
这是因为网络和线程的结合。
说明也在该页(链接)。 我也有这个,看看这个答案: 我认为这个解决方案可以奏效:
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
}
}
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.TextView;
//
公共类连接器扩展异步任务{
@凌驾
受保护的字符串doInBackground(文本视图…参数){
//TODO自动生成的方法存根
返回GetSomething();
}
私有最终字符串getSomething(){
试一试{
url=新url(“http://www.dynamiskdesign.se/ipromotionnew/json/148.json");
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“GET”);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
InputStream输入=httpURLConnection.getInputStream();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
如果(输入!=null)
{
writer=新的StringWriter();
char[]buffer=新字符[1024];
Reader Reader=新的BufferedReader(新的InputStreamReader(输入,“UTF-8”));
int n;
while((n=读卡器读取(缓冲区))!=-1){
writer.write(缓冲区,0,n);
}
input.close();
}
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
字符串jsontext=writer.toString();
}捕获(JSONException e){
Log.e(“Log_标记”,“错误解析数据”+e.toString());
}
返回字符串;
}
受保护的void onPostExecute(字符串页){
//onPostExecute
}
}
这是因为网络和线程的结合。
说明也在该页(链接)。
我也有这个,看看这个答案:
我认为这个解决方案可以奏效:
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
}
}
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.os.AsyncTask;
导入android.util.Log;
导入android.widget.TextView;
//
公共类连接器扩展异步任务{
@凌驾
受保护的字符串doInBackground(文本视图…参数){
//TODO自动生成的方法存根
返回GetSomething();
}
私有最终字符串getSomething(){
试一试{
url=新url(“http://www.dynamiskdesign.se/ipromotionnew/json/148.json");
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod(“GET”);
httpURLConnection.setDoOutput(true);
httpURLConnection.connect();
InputStream输入=httpURLConnection.getInputStream();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
如果(输入!=null)
{
writer=新的StringWriter();
char[]buffer=新字符[1024];
Reader Reader=新的BufferedReader(新的InputStreamReader(输入,“UTF-8”));
int n;
while((n=读卡器读取(缓冲区))!=-1){
writer.write(缓冲区,0,n);
}
input.close();
}
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
字符串jsontext=writer.toString();
}捕获(JSONException e){
Log.e(“Log_标记”,“错误解析数据”+e.toString());
}
返回字符串;
}
受保护的void onPostExecute(字符串页){
//onPostExecute
}
}
在哪一行引发异常?logcat中有更多信息吗?@RvdK此处不需要logcat。这是因为:这个错误来自蜂巢(3.0或更高版本)。如文档所述,您不能在其主线程上执行联网操作。若要获得此功能,必须使用handler或asynctask。哪一行是异常