【问题标题】:android Error converting result java.io.FileNotFoundExceptionandroid错误转换结果java.io.FileNotFoundException
【发布时间】:2016-03-30 17:35:53
【问题描述】:

我在 post 方法中创建了一个 API,它在 postman 中运行良好,即它给出了所需的响应。但是在 Android 中使用该 API 时会报错:

错误转换结果 java.io.FileNotFoundException 和错误解析数据 org.json.JSONException: End of input at character 0 of

如果有人指导我如何做到这一点,我将不胜感激。

这里是jsonparsermakeHttpRequest方法的代码:

public JSONObject makeHttpRequest2(String url2 , String method,
                   String name, String password) throws IOException {

  // Making HTTP request
  try {

    // check for request method
    if (method == "POST") {
      // request method is POST

      url = new URL(url2);
      conn = (HttpURLConnection) url.openConnection();
      conn.setDoOutput(true);
      conn.setRequestMethod("POST");
      conn.setUseCaches(false);
      conn.setConnectTimeout(10000);
      conn.setReadTimeout(10000);
      conn.setRequestProperty("Content-Type", "application/json");
      conn.setRequestProperty("Host", "android.schoolportal.gr");
      conn.connect();

      JSONObject jsonParam = new JSONObject();
      jsonParam.put("name", name);
      //jsonParam.put("email", email);
      jsonParam.put("password", password);
      Log.d("json",String.valueOf(jsonParam));
      OutputStreamWriter out=new OutputStreamWriter(
      conn.getOutputStream());
      out.write(jsonParam.toString());
      out.close();

    }
  } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } catch (JSONException e) {
    e.printStackTrace();
  }

  try {
    InputStream is = conn.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
      sb.append(line + "\n");
    }
    is.close();
    json = sb.toString();
    Log.e("JSON", json);
  } catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
  }
  // try parse the string to a JSON object

  try {
    jObj = new JSONObject(json);
  } catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
  }
  Log.d("json3",String.valueOf(json));
  // return JSON String
  return jObj;

}

【问题讨论】:

    标签: java android json api


    【解决方案1】:

    首先你没有检查请求是否成功,在调用conn.getInputStream();之前如果请求失败,则流是空的,你需要调用

    new BufferedReader(new InputStreamReader(connection.getErrorStream()));
    

    什么行号给您带来问题?如果您打印出 JSON 以确保响应是有效的 JSON。

    【讨论】:

      【解决方案2】:

      将此添加到您的代码中:

      conn.setDoInput(true); //After or before of setDoOutput(true) for organization :)
      conn.setChunkedStreamingMode(0);
      conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
      

      编辑:
      如果您需要为服务器上传 JSON 格式:

      conn.setRequestProperty("Content-Length", "" + Integer.toString(JsonObjectstr.getBytes().length)); // Get the json string length
      

      【讨论】:

        猜你喜欢
        • 2013-11-07
        • 2014-06-23
        • 2015-08-27
        • 2015-04-12
        • 1970-01-01
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多