我正在尝试从JSON web api获取数据。 api的URL包括ID号。如果URL具有ID号,则代码正在运行。我想要的是用户在edittext中输入ID号,然后将其添加到主URL。如果我喜欢String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" + EditTextValue;
,则进度加载器会在app打开时展开,然后没有显示结果。此外(在主代码中)结果(ID的名称,移动电话和电子邮件)在textview中显示为组合。可以用3种不同的edittext显示吗?
<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);
}
}
答案 0 :(得分:1)
您的基本网址应如下所示:
String baseURL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/";
当您从editText
检索用户输入的值时,请在开始阅读JSON之前检查它是否为空
if (ed1.getText().toString().matches("")) {
// prompt the user to tell that the edittext is empty
} else {
String url = baseURL + ed1.getText().toString();
// ans start reading your JSON
}
答案 1 :(得分:0)
这并不奇怪。 Edittext实例不代表其中的文本。它将为您提供有关它的类的信息,例如实例ID而不是其中的文本。这就是方法toString() 要查看它现在传递给url的内容,只需添加类似的内容即可我试过,String URL = “http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/”+ ED1; (其中Ed1是edittext对象)
Toast.makeText(Load_List.this, URL, Toast.LENGTH_SHORT).show();
行后
String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" +Ed1;
为了使它成功,你需要使用
String URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" +Ed1.getText().toString();