2013-01-11 145 views
0

如果標題格式不正確,很抱歉。基本上這是我的問題:我的Android項目是一個REST客戶端。 webservice項目包含一些基本上爲個人設置個人資料的數據(名字,姓氏等)。在我的Android客戶端中,我有一個具有文本字段和一個按鈕的活動。主要活動中的文本字段是用戶輸入帳號(id = enter_acct)的地方,該帳號指向具體人員,其詳細信息位於webservice項目中。在我的MainActivity java文件中,我告訴按鈕打開一個包含文本字段的新活動。換句話說,主要活動包含執行GET請求的所有代碼。輔助活動只是一個包含文本字段的佈局。這是我的主要佈局文件代碼:GET REST客戶端上的Android REST客戶端未填充文本字段

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    ...... 
/> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" /> 

<EditText 
    android:id="@+id/enter_acct" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView1" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="20dp" 
    android:layout_marginLeft="34dp" 
    android:ems="10" 
    android:hint="@string/acct" /> 

<Button 
    android:id="@+id/search_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/textView1" 
    android:layout_toRightOf="@+id/textView1" 
    android:text="@string/searchText" 
    android:onClick="retrievePersonData" 
    /> 

這裏是在主Activty的java文件,其中包含該方法retrievePersonData代碼:

public class MainActivity extends Activity { 
private static final String SERVICE_URL = "http://serverIP"; 
private static final String TAG = "MainActivity"; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    try { 

     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 

     Button btnOpenNewActivity = (Button) findViewById(R.id.search_button); 
     btnOpenNewActivity .setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 

     Intent myIntent = new Intent(MainActivity.this,PersonModel.class); 

     MainActivity.this.startActivity(myIntent); 

     } 

     }); 
     } 
     catch (Exception e) 
     { 

     } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 



public void handleResponse(String response) { 
    EditText edFirstName = (EditText) findViewById(R.id.firstName); 
    EditText edLastName = (EditText) findViewById(R.id.lastName); 
    EditText edEmail = (EditText) findViewById(R.id.email); 
    EditText edAddress = (EditText) findViewById(R.id.address); 

    edFirstName.setText(""); 
    edLastName.setText(""); 
    edEmail.setText(""); 
    edAddress.setText(""); 

    try { 
     JSONObject jso = new JSONObject(response); 

     String firstName = jso.getString("firstName"); 
     String lastName = jso.getString("lastName"); 
     String email = jso.getString("email"); 
     String address = jso.getString("address"); 

     edFirstName.setText(firstName); 
     edLastName.setText(lastName); 
     edEmail.setText(email); 
     edAddress.setText(address); 

    } catch (Exception e) { 
     Log.e(TAG, e.getLocalizedMessage(), e); 

    } 
} 

public void hideKeyboard() { 
    InputMethodManager inputManager = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
} 

public class WebServiceTask extends AsyncTask<String, Integer, String> { 
    public static final int GET_TASK = 1; 
    public static final int POST_TASK = 2; 

    private static final String TAG = "WebServiceTask"; 

    // connection timeout in milliseconds.. waiting for connect 
    private static final int CONN_TIMEOUT = 3000; 

    // socket timeout, in milisecs (waiting for data)... 
    private static final int SOCKET_TIMEOUT = 5000; 

    private int taskType = GET_TASK; 
    private Context mContext = null; 
    private String processMessage = "Processing..."; 

    private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(); 

    private ProgressDialog pDlg = null; 

    public WebServiceTask(int taskType, Context mContext, String processMessage) { 
     this.taskType = taskType; 
     this.mContext = mContext; 
     this.processMessage = processMessage; 

    } 

    public void addNameValuePair(String name, String value) { 
     params.add(new BasicNameValuePair(name, value)); 
    } 

    private void showProgressDialog() { 
     pDlg = new ProgressDialog(mContext); 
     pDlg.setMessage(processMessage); 
     pDlg.setProgressDrawable(mContext.getWallpaper()); 
     pDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     pDlg.setCancelable(false); 
     pDlg.show(); 
    } 

    @Override 
    protected void onPreExecute() { 

     hideKeyboard(); 
     showProgressDialog(); 

    } 

    protected String doInBackground(String... urls) { 
     String url = urls[0]; 
     String result = ""; 

     HttpResponse response = doResponse(url); 

     if(response == null) { 
      return result; 
     } else { 
      try { 

       result = inputStreamToString(response.getEntity().getContent()); 

      } catch (IllegalStateException e) { 
       Log.e(TAG, e.getLocalizedMessage(), e); 

      } catch (IOException e) { 
       Log.e(TAG, e.getLocalizedMessage(), e); 
      } 
     } 

     return result; 


    } 

    @Override 
    public void onPostExecute(String response) { 

     handleResponse(response); 
     pDlg.dismiss(); 

    } 

    public HttpParams getHttpParams() { 

     HttpParams httpa = new BasicHttpParams(); 

     HttpConnectionParams.setConnectionTimeout(httpa, CONN_TIMEOUT); 
     HttpConnectionParams.setSoTimeout(httpa, SOCKET_TIMEOUT); 

     return httpa; 
    } 

    public HttpResponse doResponse(String url) { 

    HttpClient hClient = new DefaultHttpClient(getHttpParams()); 

    HttpResponse response = null; 

    try { 
     switch (taskType) { 

     case POST_TASK: 
      HttpPost httppost = new HttpPost(url); 
      httppost.setEntity(new UrlEncodedFormEntity(params)); 
      response = hClient.execute(httppost); 
      break; 
     case GET_TASK: 
      HttpGet httpget = new HttpGet(url); 
      response = hClient.execute(httpget); 
      break; 
     } 

    } catch (IOException e) { 
     Log.e(TAG, e.getLocalizedMessage(), e); 
    } 
    return response; 
    } 

    private String inputStreamToString(InputStream is) { 

     String line = ""; 
     StringBuilder total = new StringBuilder(); 

     BufferedReader bf = new BufferedReader(new InputStreamReader(is)); 

     try { 
      while ((line = bf.readLine()) != null) { 
       total.append(line); 
     } 
    } catch (IOException e) { 
     Log.e(TAG, e.getLocalizedMessage(), e); 
    } 
    return total.toString(); 
    } 


} 

當我點擊按鈕,它應該做兩件事:

  1. 用文本字段打開一個新的活動。

  2. 用webservice項目中的信息填充這些文本字段。

,如果我有一個文本框和一個按鈕一個活動這只是正常(即我點擊它談論的Web服務,並與信息填充字段中的按鈕)。在這種情況下,我點擊按鈕,它會打開新的活動,但不會填充文本字段......它什麼都不做。

有人可以請幫助,如果需要更清晰我會。任何幫助是極大的讚賞。

+0

張貼您的代碼,那麼我們可以看到它去錯了... –

+0

好吧,我編輯了原來的職位有代碼。我想說的是,爲什麼點擊按鈕時,按鈕不能做兩件事? ...開展新的活動並填充新活動中的字段。 – MaxK

回答

1

您試圖在您的UI代碼中運行HTTP請求,這就是爲什麼你有強制關閉。請嘗試運行在服務或AysncTask或線程,所以UI線程不會被阻止。

請參閱this example

您可以在按鈕點擊執行多個操作,火GET方法,以上技術和startActivity:

public class PersonModel extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     EditText firstNameTxt= (EditText) findViewById(R.id.editText1); 
     firstNameTxt.setText("set your value"); 
    } 
} 

嘗試使用的setText在填充值的的EditText PersonModel中的OnCreate方法。

感謝

+0

我會嘗試,但它必須連接到服務器,並做GET請求..我只是不明白爲什麼一個按鈕不能做兩件事...打開一個新的活動,並運行一種方法,即似乎是如此基本和簡單的事情要做。 – MaxK

+0

這不起作用。現在只要按下按鈕就會崩潰。 – MaxK

+0

我更新了我的答案,請檢查並重試 –