尝试读取url连接时出现java.io.FileNotFoundException

时间:2017-03-01 00:36:07

标签: java android ruby-on-rails

简单地说,我在AsyncTask中向服务器发送POST请求。服务器发送回JSON响应,我想将其读入输入流:

protected String doInBackground(Map<String, String>... params) {

        String result = "";
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(OMNI_AUTH_CALLBACK);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setReadTimeout(10000);
            urlConnection.setConnectTimeout(15000);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);

            OutputStream os = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));


            writer.write(getQuery(params[0]));


            writer.flush();
            writer.close();
            os.close();

            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while(data != -1) {
                char current = (char) data;
                result += current;
                data = reader.read();
            }
        } catch(Exception e){
            e.printStackTrace();
        }

        return result;
    }

我的调试器显示此行发生错误:

InputStream in = urlConnection.getInputStream();

完整堆栈跟踪:

W/System.err: java.io.FileNotFoundException: http://10.0.2.2:3001/users/auth/facebook/callback.json
W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
W/System.err:     at io.menuloop.menuloopandroid.LoginActivity$SaveUserFromOAuth.doInBackground(LoginActivity.java:301)
W/System.err:     at io.menuloop.menuloopandroid.LoginActivity$SaveUserFromOAuth.doInBackground(LoginActivity.java:270)
W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err:     at java.lang.Thread.run(Thread.java:818)
W/System.err: org.json.JSONException: End of input at character 0 of 
W/System.err:     at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
W/System.err:     at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:156)
W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)
W/System.err:     at io.menuloop.menuloopandroid.LoginActivity$SaveUserFromOAuth.onPostExecute(LoginActivity.java:346)
W/System.err:     at io.menuloop.menuloopandroid.LoginActivity$SaveUserFromOAuth.onPostExecute(LoginActivity.java:270)
W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:636)
W/System.err:     at android.os.AsyncTask.access$500(AsyncTask.java:177)
W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err:     at android.os.Looper.loop(Looper.java:135)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是一个重要的观点。当模型没有保存在我的Rails应用程序中时,我发回422:

render json: { error: @user.errors.full_messages }, status: 422

注意我发送数据和那个422.那个数据就是我想从输入流中读取的数据。

int status = urlConnection.getResponseCode();
// 422

我可能做错了什么?

0 个答案:

没有答案