【发布时间】:2015-05-29 07:48:13
【问题描述】:
我正在尝试从 JSON Web api 获取数据。 api 的 URL 包括 ID 号。如果 URL 具有 ID 号,则代码有效。我想要的是用户在 edittext 中输入 ID 号,然后将其添加到主 URL。如果我喜欢String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" + EditTextValue;,进度加载器会在应用程序打开时旋转,然后没有显示结果。此外(在主代码中)结果(ID 的姓名、手机和电子邮件)在 textview 中显示为组合。可以在 3 种不同的编辑文本中显示吗?
<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="com.example_load_list.Load_List$PlaceholderFragment" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView"
android:textSize="20sp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/tv"
android:layout_marginTop="37dp"
android:ems="10"
android:hint="Enter Booking ID"
android:inputType="phone" />
</RelativeLayout>
public class Load_List extends Activity {
ArrayList<String> AD_C_List;
ProgressDialog P_Dialog_TR;
ArrayAdapter<String> ARR;
AutoCompleteTextView AV;
TextView TV;
EditText ed1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_load_list);
TV = (TextView)findViewById(R.id.tv);
//ed1 = (EditText)findViewById(R.id.editText1);
String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/51610002";
loading_data(URL);
}
private void loading_data(String url) {
// TODO Auto-generated method stub
AD_C_List = new ArrayList<String>();
P_Dialog_TR = new ProgressDialog(Load_List.this);
P_Dialog_TR.setMessage("Fetching Data...");
P_Dialog_TR.setCancelable(true);
P_Dialog_TR.show();
RequestParams params = new RequestParams();
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, params, new JsonHttpResponseHandler() {
public void onFailure(int statusCode, Throwable e,
JSONObject errorResponse) {
// TODO Auto-generated method stub
//super.onFailure(statusCode, e, errorResponse);
//This will called on 4xx HTTP error
Toast.makeText(getApplicationContext(), "No data", Toast.LENGTH_SHORT).show();
System.out.println("Status Code ::: "+ statusCode);
System.out.println("Error Response ::: "+ errorResponse);
}
@Override
public void onSuccess(int statusCode,
org.apache.http.Header[] headers, final JSONObject response) {
// TODO Auto-generated method stub
// super.onSuccess(statusCode, headers, response);
System.out.println("Success Response ::: "+response);
P_Dialog_TR.dismiss();
try {
JSONObject jsonObject = response.getJSONObject("GetAssignedStaffResult");
String e_name = jsonObject.getString("EMAIL");
String m_name = jsonObject.getString("MOBILE");
String u_name = jsonObject.getString("UserName");
String final_txt = " Email: "+e_name+"\n Mobile :"+m_name+"\n Username :"+u_name;
System.out.println("JSON Object: "+jsonObject);
Log.d("Email : ",jsonObject.getString("EMAIL"));
Log.i("Mobile :",jsonObject.getString("MOBILE"));
Log.i("Username :",jsonObject.getString("UserName"));
for (int i = 0; i < AD_C_List.size(); i++) {
String item = AD_C_List.get(i);
Log.i("Array List",item);
}
//Toast.makeText(getApplicationContext(), u_name, Toast.LENGTH_SHORT).show();
populate_TV(final_txt);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void populate_TV(String C_List) {
TV.setText(C_List);
}
}
【问题讨论】:
-
向我们展示如何检索 EditText 的值。我确定错误在那里
-
我试过了,
String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" + Ed1;(其中 Ed1 是 edittext 对象)
标签: android json web-services