【问题标题】:JSON Parsing not working for edittextJSON解析不适用于edittext
【发布时间】: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


【解决方案1】:

您的基本 URL 应该是这样的:

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
}

【讨论】:

  • @metalurgus,我试过你的代码,主要问题仍然是进度加载器围绕打开应用程序而不是让用户有机会在 edtitext 中输入值,然后添加总 URL 然后在 textview 中显示结果。
【解决方案2】:

我试过了,字符串 URL = "http://103.8.127.248:1002/serv1/Service1.svc/getassignedstaff/" + 编辑1; (其中 Ed1 是编辑文本对象)

这并不奇怪。 Edittext 实例不代表其中的文本。它将为您提供有关其类的信息以及实例 ID 之类的信息,而不是其中的文本。这就是 toString() 方法的全部内容。
要查看它现在传递给 url 的内容,只需添加类似

的内容
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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多