我正在尝试使用 webapi
从 mvc 4 应用程序获取 json 文件
但是我在 asynctask
doinbackground
方法中出错
java.lang.illegalargumentexception host may not be null
当我跟踪我的代码时,我发现它在这段代码最后一行的 url 中的 getjson 中的 jsonparser 上崩溃了
public JSONObject getJSONFromUrl(String Url) throws JSONException
{
try {
Log.d("tony","6"+Url);
DefaultHttpClient httpClient = new DefaultHttpClient(); // to connect to http
Log.d("tony","7"+Url);
HttpGet httpGet=new HttpGet(Url);
Log.d("tony","8"+httpGet.getMethod());
HttpResponse httpResponse = httpClient.execute(httpGet);
这是我在后台代码中做的
public JSONObject doInBackground(String... urls) {
// get url pointing to entry point of API
String address = urls[0].toString();
if (method.equals(LOAD_REQUEST))
{
return getJSON(address);
}
else
return makeHttpRequest(address, method, parameters);
}
获取json方法
public JSONObject getJSON(String url){
JSONObject j = null;
try {
j=parser.getJSONFromUrl(url);
} catch (JSONException e) {
e.printStackTrace();
}
return j;
}
最佳答案
改变
localhost:2100/api/products
到
http://localhost:2100/api/products
关于android - 致命异常 asyncTask #1 主机可能不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25189550/