2013-12-12 94 views
0

嗨請有人可以幫我看看這段代碼嗎?不知道做錯了什麼,但try塊不運行。相反,它會進入catch塊。Android登錄無效

public void onClick(View arg0) { 
    //Toast.makeText(getBaseContext(), "connecting",Toast.LENGTH_SHORT).show(); 
    // TODO Auto-generated method stub 
    httpclient = new DefaultHttpClient(); 
    htpost = new HttpPost("http://10.0.2.2/fanaticmobile/log_in.php"); 
    uname= username.getText().toString(); 
    pass= password.getText().toString(); 

    try { 
     namearray = new ArrayList<NameValuePair>(); 

     namearray.add(new BasicNameValuePair("username", uname)); 
     namearray.add(new BasicNameValuePair("password", pass)); 
     htpost.setEntity(new UrlEncodedFormEntity(namearray)); 
     response= httpclient.execute(htpost); 
     if(response.getStatusLine().getStatusCode()==200){ 
      entity= response.getEntity(); 

      if(entity != null){ 
       InputStream stream = entity.getContent(); 

       JSONObject jresponse = new JSONObject(ConvertInput(stream)); 
       String logged= jresponse.getString("logged"); 
       login_err.setText(""+logged); 
       if(logged.equals("true")){ 
        Toast.makeText(getBaseContext(), "Successfull",Toast.LENGTH_SHORT).show(); 
        //String retname= jresponse.getString("name"); 
        //String retmail= jresponse.getString("email"); 
       }else if(logged.equals("false")){ 
        String message=jresponse.getString("message"); 
        Toast.makeText(getBaseContext(), message,Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }else{ 

     } 

    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     Toast.makeText(getBaseContext(), "Poor Connection",Toast.LENGTH_SHORT).show(); 
    } 

}// 

這是閱讀JSON對象

private static String ConvertInput(InputStream is){ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line =""; 

    try { 
     while((line = reader.readLine())!= null){ 
      sb.append("\n"); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    }finally{ 
     try { 
      is.close(); 
     } catch (IOException e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
}// end of convert function 

請是新來這個,我跟着一個教程,這點功能,但我不工作。已經在清單文件中設置了權限(互聯網)

+2

請從您的logcat發佈例外。這將包含爲什麼它不起作用的線索。 –

+2

'response = httpclient.execute(htpost);'Google for'NetworkOnMainThreadException' – Raghunandan

+0

@KenWolf logcat太長了。有其他發佈方式嗎? – altoin

回答

0

我有一個建議嘗試使用AsyncHttpclient從服務器獲取響應不需要這樣長碼。

http://loopj.com/android-async-http/

AsyncHttpClient asyncHttpClient=new AsyncHttpClient(); 
RequestParams params=new RequestParams(); 
     params.put("username", uname); 
     params.put("password", pass); 
      asyncHttpClient.post("http://10.0.2.2/fanaticmobile/log_in.php", params,new AsyncHttpResponseHandler(){ 
       @Override 
       public void onFailure(Throwable arg0, String arg1) { 
        // TODO Auto-generated method stub 
        super.onFailure(arg0, arg1); 
       } 
       @Override 
       public void onSuccess(String arg0) { 
        // TODO Auto-generated method stub 
        super.onSuccess(arg0); 
       } 
      }); 

只需在項目中包含的jar文件將是簡單易用。

+0

@Aswin S Ashok。謝謝。我會盡力回覆你。在哪個文件夾中,我把新的jar文件? – altoin

+0

在libs文件夾中,如果不存在,則創建該文件夾。告訴我,如果它不工作。 –

+0

嗨@ASwin S Ashok在這裏得到錯誤「public void onFailure(Throwable arg0,params,String arg1)」告訴我逗號是預期的。請如果它不會太多問,你能幫我重新考慮我的代碼?已經將jar文件下載到我的libs文件夾中。 – altoin

0

就像已經在註釋中聲明的那樣,您正在主線程(UI線程)中運行網絡操作。這不僅是不鼓勵的(長時間的操作應該是永不使用主線程),而且禁止在網絡的情況下。

response= httpclient.execute(htpost) 

^此失敗。

閱讀如何將該代碼移動到AsyncTask,並在官方谷歌參考中以正確的方式執行該操作。谷歌搜索AsyncTask也會有所幫助。

一個僞代碼版本是:

public class YourTask extends AsyncTask<Void, Void, Void>{ 
    YourListener mListener; 
    public YourTask(final YourListener listener) { 
     mListener = listener; 
    } 
    @Override 
    protected Void doInBackground(final Void... params) { 
     // do your lengthy operation here 
     return null; 
    } 
    @Override 
    protected void onPostExecute(Void result) { 
     mListener.onVeryLongTaskDone(); 
    } 
    public interface YourListener { 
     public void onVeryLongTaskDone(); 
    } 
} 

然後讓你的活動執行該「YourListener」界面和方法onVeryLongTaskDone()將被調用。

你如何開始任務?

在你的onClick方法:

(new YourTask(YourActivityName.this)).execute(); 
+0

謝謝,我現在嘗試代碼 – altoin