android - 致命异常 asyncTask #1 主机可能不为空

标签 android json android-asynctask

我正在尝试使用 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/

相关文章:

Android:如何确定 TextView 中最后一个完全可见的字母?

android - 如何使用 API 21 android 创建相机预览?

android - 如何使用 Espresso 检查屏幕外 View 的可见性?

android - 如何在 Android 中解析 JSON 对象

c# - Json 格式的字符串列表

java - 数据未存储到领域数据库中

Android ACTION_UP 和 ACTION_MOVE 在动态壁纸 AndEngine 中不起作用

java - 是否可以使用面向 GWT RPC 的命令模式而不使用 RPC 序列化?

Android - 如何在一个 Activity 中进行两个异步调用?

android - 动态更改 ViewPager 中的 fragment 内容