2
Webサービスから投稿として登録ダミーを送信して接続を確認しようとしています。しかし、私はそうすることができません。私は行くことができましたさらには、以下に見られることができます。FileNotFoundExceptionを使用してWebサービスにPOSTを送信するHttpURLConnection
@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", "[email protected]");
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();
}
}
は、Androidモニターからのログの下に検索しますWebサービス
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: [email protected] 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");
}
多くの感謝!
完璧な答えに、これは、これは単なる特派活動を開くことによって、呼び出されただけで、接続テストであるとして、そのことについて申し訳ありませんが、コード –
を実行して提供してください保護されたvoid onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_registration); r =新しいRegisterRequestTask(this); r.execute( "接続URL"); } } – Fake
よろしくお願いします。渡されたURLをparams [0]として確認できます。 !あなたのログによるとあなたのURLは動作していません:) –