0
I have a problem that I can not solve T_T, after many searches I created a web service (test), to be consumed by an android application.
The only function of it is to return a String, but I don’t know why it doesn’t work, because the String even appears in the logcat, I did all the way, but no arrow in the TextView
.
If you can give me a hand, I’ll be very grateful, here comes 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 connecting with 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 Too Much 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 Casing 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.consultsitio.ConsultSituacao.interpretaresult(Query.java:57) 07-27 10:56:20.030: W/System.err(1790): at com.example.consultsitio.ConsultSituacao.doInBackground(Query.java:35) 07-27 10:56:20.030: W/System.err(1790): at com.example.consultsitio.ConsultSituacao.doInBackground(Query.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)
I found the error /o/. I changed the method: private String interpreted result(String result) throws Jsonexception { Jsonobject Object = new Jsonobject(); Object.getJSONObject(result); Return "The returned text is:" + result; to: private String interpreted result(String output) throws Jsonexception { Return result; ________________ XD, Obg.
– OsírisAguiar
Ah... I was mounting the answer while posting that solved the problem =/ But I think your attempt just slows down the error... Take a look at my answer.
– Wakim
Thank you very much, I will look at it carefully. D
– OsírisAguiar
Oh I got it. My WS was not generating a JSON, but just a string. So when I pulled those codes out, it was bad, but not the way I expected. However, after adding its changes, WS started to correctly generate a JSON, and Android managed to interpret. XD. I only had to change the (char[22]) to (char[39]). Thank you very much!
– OsírisAguiar