HttpURLConnection使用FileNotFoundException将POST发送到Web服务

时间:2016-08-21 18:42:25

标签: android json httpurlconnection filenotfoundexception outputstream

我正在尝试将注册虚拟邮件发送到Web服务,以检查连接。但是我无法这样做。我能够走得越远,可以在下面看到:

@Override
protected String doInBackground(String... params) {
    try {
        Log.i("tst", "Teste0");
        URL url = new URL(params[0]);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.connect();
        ;
        JSONObject usuario = new JSONObject();
        usuario.put("nome", "Pelé");
        usuario.put("email", "pele@uol.com.br");
        usuario.put("senha", "qwerty");
        out = new DataOutputStream(urlConnection.getOutputStream());
        out.writeBytes(URLEncoder.encode(usuario.toString(), "UTF-8"));
        out.flush();
        out.close();
        InputStream conteudo = urlConnection.getInputStream();
        json = Util.toString(conteudo);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return json;
}

在下面找到Util类以从服务器获取返回:

public class Util {
    public static String toString(InputStream is) throws Exception{

        BufferedInputStream in = new BufferedInputStream(is);
        InputStreamReader r = new InputStreamReader(in, "UTF-8");
        StringWriter w = new StringWriter();
        int v = r.read();

        while (v != -1) {
            w.write(v);
            v = r.read();
        }

        return w.toString();
    }
}

尝试连接到Web服务时,从Android Monitor查找以下日志。

08-21 15:16:41.451 12155-12287/br.com.fiap.petwell W/System: ClassLoader referenced unknown path: /system/framework/tcmclient.jar
08-21 15:16:41.526 12155-12226/br.com.fiap.petwell D/OpenGLRenderer: endAllActiveAnimators on 0xa09d3280 (RippleDrawable) with handle 0xaea027a0
08-21 15:16:41.541 12155-12155/br.com.fiap.petwell I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@1dbcd27 time:127326360
08-21 15:16:41.560 12155-12287/br.com.fiap.petwell W/System.err: java.io.FileNotFoundException: *URL used to connection here*
08-21 15:16:41.561 12155-12287/br.com.fiap.petwell W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
08-21 15:16:41.561 12155-12287/br.com.fiap.petwell W/System.err:     at br.com.fiap.petwell.requesttask.RegisterRequestTask.doInBackground(RegisterRequestTask.java:55)
08-21 15:16:41.561 12155-12287/br.com.fiap.petwell W/System.err:     at br.com.fiap.petwell.requesttask.RegisterRequestTask.doInBackground(RegisterRequestTask.java:20)
08-21 15:16:41.561 12155-12287/br.com.fiap.petwell W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
08-21 15:16:41.562 12155-12287/br.com.fiap.petwell W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-21 15:16:41.562 12155-12287/br.com.fiap.petwell W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
08-21 15:16:41.562 12155-12287/br.com.fiap.petwell W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
08-21 15:16:41.562 12155-12287/br.com.fiap.petwell W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
08-21 15:16:41.563 12155-12287/br.com.fiap.petwell W/System.err:     at java.lang.Thread.run(Thread.java:818)
08-21 15:16:41.565 12155-12155/br.com.fiap.petwell I/teste: teste2

执行代码,按要求:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registration);
    r = new RegisterRequestTask(this);
    r.execute("Connection URL");
}

非常感谢!

1 个答案:

答案 0 :(得分:1)

找到一种方法,使用OutputStreamWriter而不是DataOutputStream。 代码调整如下:

@Override
protected String doInBackground(String... params) {
    try {
        Log.i("tst", "Teste0");
        URL url = new URL(params[0]);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.connect();
        JSONObject usuario = new JSONObject();
        usuario.put("nome", "Pelé");
        usuario.put("email", "pele@uol.com.br");
        usuario.put("senha", "qwerty");
        out = new OutputStreamWriter(urlConnection.getOutputStream());
        out.write(usuario.toString());
        out.flush();
        out.close();
        InputStream conteudo = urlConnection.getInputStream();
        json = Util.toString(conteudo);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return json;
}