I have a problem that I can not solve T_T, after many searches I created a webservice (test), to be consumed by an android application.
The only function of it is to return a String, but I do not know why it does not work, because the String even appears in the logcat, I did all the way, but no arrow in TextView
.
If you can help me, I'll be very grateful, here's the code:
WebService:
package com.rodasdo;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
@Path("/oi")
public class Trator {
@GET
@Produces("application/json")
public String returnTitle(){
return "Tripa de Galinha azeda";
}
}
MainActivity Android:
package com.example.consultasitio;
import com.example.consultasitio.ConsultaSituacao.ConstultaSituacaoSitioListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity implements ConstultaSituacaoSitioListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ConsultaSituacao(this).execute();
}
@Override
public void onConsultaConcluida(String situacaositio) {
// TODO Auto-generated method stub
TextView text = (TextView) findViewById(R.id.what);
text.setText(situacaositio);
}
}
Class that connects to ws:
package com.example.consultasitio;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
public class ConsultaSituacao extends AsyncTask<Void, Void, String> {
private ConstultaSituacaoSitioListener listener;
private static final String URL_STRING = "http://192.168.1.9:8080/Servidorincompleto/oi";
public ConsultaSituacao (ConstultaSituacaoSitioListener listener) {
this.listener=listener;
}
@Override
protected String doInBackground(Void... params) {
try {
String resultado = consultaServidor();
return interpretaresultado(resultado);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return null;
}
private String interpretaresultado(String resultado) throws JSONException {
JSONObject object = new JSONObject();
object.getJSONObject(resultado);
return "O texto que voltou é:" + resultado;
}
private String consultaServidor() throws IOException {
InputStream is = null;
try {
URL url = new URL(URL_STRING);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(1000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(false);
conn.connect();
conn.getResponseCode();
is=conn.getInputStream();
Reader reader = new InputStreamReader(is);
char[] buffer = new char[22];
reader.read(buffer);
return new String(buffer);
} finally {
if (is !=null){
is.close();
}
}
}
@Override
protected void onPostExecute(String result) {
listener.onConsultaConcluida(result);
}
public interface ConstultaSituacaoSitioListener {
void onConsultaConcluida(String situacaositio);
}
}
Xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="@+id/what"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Logcat error:
07-27 10: 56: 19.210: I / Choreographer (1790): Skipped 48 frames! The application may be doing a lot of work on its main thread. 07-27 10: 56: 19,970: D / dalvikvm (1790): GC_FOR_ALLOC freed 241K, 10% free 2964K / 3272K, paused 112ms, total 112ms 07-27 10: 56: 20,020: W / System.err (1790): org.json.JSONException: No value for Sour Chicken Gut 07-27 10: 56: 20,030: W / System.err (1790): at org.json.JSONObject.get (JSONObject.java:355) 07-27 10: 56: 20,030: W / System.err (1790): at org.json.JSONObject.getJSONObject (JSONObject.java:574) 07-27 10: 56: 20,030: W / System.err (1790): at com.example.consult.site.Consult.Situation.interpret result (QuerySituation.java:57) 07-27 10: 56: 20,030: W / System.err (1790): at com.example.consult.site.Consult.InBackgroundSituation.Insituation.java:35 07-27 10: 56: 20,030: W / System.err (1790): at com.example.consult.site.Consult.InBackgroundSituation.Insituation.java:1 07-27 10: 56: 20,030: W / System.err (1790): at android.os.AsyncTask $ 2.call (AsyncTask.java:288) 07-27 10: 56: 20,030: W / System.err (1790): at java.util.concurrent.FutureTask.run (FutureTask.java:237) 07-27 10: 56: 20,030: W / System.err (1790): at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:231) 07-27 10: 56: 20,030: W / System.err (1790): at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1112) 07-27 10: 56: 20,030: W / System.err (1790): at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:587) 07-27 10: 56: 20,030: W / System.err (1790): at java.lang.Thread.run (Thread.java:841)