【发布时间】:2016-12-28 07:18:30
【问题描述】:
我正在尝试将注册虚拟对象作为帖子发送到 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");
}
非常感谢!
【问题讨论】:
-
完美答案请提供执行代码
-
很抱歉,这只是一个连接测试,只需打开对应的活动即可调用:`@Override` protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_registration); r = new RegisterRequestTask(this); r.execute("连接 URL"); } }
-
好的!您可以检查您作为参数 [0] 传递的网址。因为!根据您的日志,您的网址无效:)
-
实际上,我尝试通过 get(浏览器)和使用邮递员的 post 进行检查,并且效果很明显。这让我认为这是 android 上的一个问题 =/
-
然后您可以检查将输入流字符集更改为 ISO-8859-1
标签: android json httpurlconnection filenotfoundexception outputstream