2

Ich versuche, einen Registrierungsdummy als Post von einem Webdienst zu senden, um die Verbindung zu überprüfen. Aber ich kann das nicht tun.HttpURLConnection Senden von POST an einen Webdienst mit FileNotFoundException

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

unter Util Klasse Rückkehr vom Server zu erhalten, finden: Je weiter ich habe in der Lage zu gehen, kann unten gesehen werden

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

unter dem Protokoll von Android-Monitor finden, wenn sie versuchen zu verbinden der Webdienst


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 

Execution-Code, wie gewünscht:

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

Vielen Dank!

+0

Um eine perfekte Antwort geben Sie bitte Code –

+0

Traurig über das ausführen, da dies nur ein Verbindungstest ist dies nur durch Öffnen der entsprechenden Aktivität genannt wird: '' @Override protected void onCreate (Paket savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_registration); r = neu RegisterRequestTask (this); r.execute ("Verbindungs-URL"); } } – Fake

+0

Okay! Sie können Ihre URL überprüfen, die Sie als Parameter [0] übergeben haben. Weil! Laut deinem Log funktioniert deine URL nicht :) –

Antwort

1

Einen Weg gefunden, mit OutputStreamWriter über DataOutputStream. Der Code wie folgt angepasst:

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