【问题标题】:Android HttpUrlConnection FileNotFoundException. Connect to http and pass paramsAndroid HttpUrlConnection FileNotFoundException。连接到 http 并传递参数
【发布时间】:2016-02-24 15:00:49
【问题描述】:
public class GeoRegistration extends AsyncTask<Void, Void, AuthorizationResponseContainer> {

private Activity activity;
private static final String USERNAME = "username=";
private static final String EMAIL = "&email=";
private static final String PASSWORD = "&password=";
private static final String PASSWORD_REPEAT = "&password_repeat=";
private String route;
private String paramsSequence;

public GeoRegistration() {
}

public GeoRegistration(Activity activity, String username, String email, String password, String passwordConfirm) {
    this.activity = activity;
    this.route = activity.getResources().getString(R.string.domain) + activity.getResources().getString(R.string.registration_second_step_route);
    this.paramsSequence = USERNAME + username +
            EMAIL + email +
            PASSWORD + password +
            PASSWORD_REPEAT + passwordConfirm;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected AuthorizationResponseContainer doInBackground(Void... params) {
    String message = null;
    int code = 0;
    HttpURLConnection httpURLConnection = null;
    try {
        URL url = new URL(route);
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setReadTimeout(10000);
        httpURLConnection.setConnectTimeout(15000);
        httpURLConnection.setRequestMethod("POST"); //Sometimes (it's very strange, I catch ProtocolExeption: Connection already established)
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);

        /*PrintWriter out = new PrintWriter(httpURLConnection.getOutputStream());
        out.print(this.paramsSequence);
        out.close();*/
        OutputStream os = httpURLConnection.getOutputStream(); 
        OutputStreamWriter osw = new OutputStreamWriter(os);
        osw.write(paramsSequence);
        osw.flush();
        osw.close();

        httpURLConnection.connect();
        int status = httpURLConnection.getResponseCode();
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); //FileNotFounException
        StringBuilder builder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line + "\n");
        }
        JSONObject jsonObject = new JSONObject(builder.toString());
        message = jsonObject.getString(ResponseKeys.MESSAGE);
        code = jsonObject.getInt(ResponseKeys.CODE);
        switch (status) {
            case 200:
                if (code == 0) {

                }
                return null;
            case 400:
                return new AuthorizationResponseContainer(ResponseKeys.CODE_400, code, message, null);
            case 500:
                return new AuthorizationResponseContainer(ResponseKeys.CODE_500, code, message, null);
        }
    } catch (IOException | JSONException e) {
        e.printStackTrace();
    } finally {
        if (httpURLConnection != null) {
            try {
                httpURLConnection.disconnect();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    return null;
}

路线:http://geo.binno.com.ua/api/m1/registration/ 为什么我无法从 HttpUrlConnection 获取输入流?我抓住了 FileNotFoundException。但是当我在浏览器的 REST API CLIENT 上做同样的事情时,我可以连接到主机并接收 json 答案。我做错了什么?

【问题讨论】:

  • 移除 httpURLConnection.connect();您已经连接并已写入输出流。

标签: android post httpurlconnection filenotfoundexception


【解决方案1】:

添加这几行代码..

httpURLConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
httpURLConnection.setRequestProperty("Accept", "*/*");

try {
    is = connection.getInputStream();
} catch(FileNotFoundException exception){
    log.error(exception.getMessage(), exception);
    is = connection.getErrorStream();
}
 BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));

我希望这能解决您的问题。谢谢...

【讨论】:

    【解决方案2】:

    我作为字符串传递了 http 参数,需要作为 JSON - 这是第一个。 当服务器 ansver 4xx 代码 - 需要获取 ErrorStream,而不是 InputStream 示例:

    int respCode = conn.getResponseCode();
    Bufferreader reader;
    switch(respCode){
    case 200:
    reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); //to avoid FNFExeption
    break;
    case 400:
    reader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); //to avoid FNFExeption
    break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      相关资源
      最近更新 更多