2

웹 서비스에 대한 게시로 등록 더미를 보내 연결을 확인하려고합니다. 그러나 나는 그렇게 할 수 없다. 내가 갈 수 있었던 더 아래 볼 수 있습니다 :에 연결하려고 할 때 FileNotFoundException을 사용하여 웹 서비스에 POST를 보내는 HttpURLConnection

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(); 
    } 
} 

안드로이드 모니터에서 로그 아래 찾기 :

@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; 
} 

서버에서 수익을 얻기 위해 백분율 클래스를 보자 웹 서비스.


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

많은 감사를!

+0

완벽한 해답이, 이것은 단지 특파원 활동을 열어라고 바로 연결 테스트이기 때문에, 그것에 대해 죄송 코드 –

+0

를 실행 제공하십시오 protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_registration); r = 새 RegisterRequestTask (this); r.execute ("연결 URL"); } } – Fake

+0

오케이! 통과 한 URL을 params [0]로 확인할 수 있습니다. 왜냐하면! 귀하의 로그에 따르면 귀하의 URL이 작동하지 않습니다 :) –

답변

1

OutputStreamWriter을 사용하여 DataOutputStream을 발견했습니다. 코드는 다음과 같이 조정 : `@Override` :

@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 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; 
} 
관련 문제