HttpURLConnection 将 POST 发送到具有 FileNotFoundException 的 Web 服务

HttpURLConnection sending POST to a Web Service with FileNotFoundException

我正在尝试将注册虚拟对象作为 post 从发送到 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 class 从服务器获取 return:

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 监视器中找到日志。


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.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.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");
}

非常感谢!

找到了一种方法,使用 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;
}