我嘗試從使用Android客戶端的Java應用程序中調用restful web服務,但每次都有相同的結果「出現一些錯誤」。 我認爲問題出在我的URL中,因爲我對從Tomcat調用Android中的restful webservice和我使用的URL沒有任何意見。 這是我的Java Main_Activity:JsonObject從本地主機獲取請求
package com.halgo.restclientandroid;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends Activity {
EditText editText;
TextView text;
Button btn;
String url ="http://localhost:8080/Stage_BGI/rest/missions/find/";
String data = "";
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
text = (TextView)findViewById(R.id.text);
btn = (Button)findViewById(R.id.btn);
requestQueue = Volley.newRequestQueue(this);
String urlJSON=url + editText.getText().toString();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
JsonObjectRequest obreq = new JsonObjectRequest(Request.Method.GET, url + editText.getText().toString(), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
/*try {
JSONObject obj = response.getJSONObject("missionObject");
String idMission = obj.getString("idMission");
String etatMission = obj.getString("etatMission");
data += "id_mission: " + idMission +
"etat_mission : " + etatMission;
text.setText(data);
}catch (JSONException e) {
e.printStackTrace();
}*/
text.setText(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
text.setText("Some error occurred!!");
}
}
);
requestQueue.add(obreq);
}
});
}
}
,這是mainActivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.halgo.restclientandroid.MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text=""/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/fetch"
android:id="@+id/btn"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text=""/>
</LinearLayout>
我找到答案在這裏 https://stackoverflow.com/questions/5495534/java-net-connectexception-localhost-127-0-0-18080-connection-refused – Halgo