2012-02-08 40 views
0

我只是想檢查從django 從android發送的字符串,然後從django發送迴應作爲字符串回到android 這段代碼工作時,我檢查它在我的web瀏覽器上,但未在Android應用程式 這是Django的Django的視圖代碼:在android中從django解析字符

def login(request): 
form = LoginForm(request.POST) 
if request.method=='POST': 
    uid=request.POST['uid'] 
    name=request.POST['name'] 
    if name=="ajinkya" : 
     if uid=='11111': 
      return HttpResponse("You are logged in...Thanks", content_type="text") 

return render_to_response('login.html',{'form':form},context_instance=RequestContext(request)) 

餘米試圖解析Android中

我的Android應用程序代碼從Django中發送的字符串是:

public class UploaddemoActivity extends Activity { 
    public String strUID; 
    public String strname; 
    public String string_response; 
    InputStream inputStream; 
    public String url; 
/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final EditText UID = (EditText) findViewById(R.id.UID); 
     UID.setOnKeyListener(new View.OnKeyListener() { 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
       (keyCode == KeyEvent.KEYCODE_ENTER)) { 
     strUID = UID.getText().toString(); 
       return true; 
       } 
       return false; 
       } 
       }); 

     final EditText nameText = (EditText) findViewById(R.id.EditText_Nickname); 
     nameText.setOnKeyListener(new View.OnKeyListener() { 
       public boolean onKey(View v, int keyCode, KeyEvent event) { 
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
       (keyCode == KeyEvent.KEYCODE_ENTER)) { 
     strname = nameText.getText().toString(); 
       return true; 
      } 
      return false; 
      } 
      });  


    Button button; 

    button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new Button.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      ArrayList<NameValuePair> vars = new ArrayList<NameValuePair>(); 
      vars.add(new BasicNameValuePair("uid",strUID)); 
      vars.add(new BasicNameValuePair("name",strname)); 
      try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       url="http://192.168.1.2/mysite/login/"; 
       HttpPost httppost = new HttpPost(url); 
       httppost.setEntity(new UrlEncodedFormEntity(vars)); 
       HttpResponse response = httpclient.execute(httppost); 
       string_response = convertResponseToString(response); 
       Toast.makeText(UploaddemoActivity.this, "Response " + string_response, Toast.LENGTH_LONG).show(); 
       }catch(Exception e){ 
       Toast.makeText(UploaddemoActivity.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       System.out.println("Error in http connection "+e.toString()); 
       } 
     } 
    }); 


} 


public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{ 
     String res = ""; 
     StringBuffer buffer = new StringBuffer(); 
    inputStream = response.getEntity().getContent(); 
    int contentLength = (int) response.getEntity().getContentLength(); //getting content length….. 
    Toast.makeText(UploaddemoActivity.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show(); 
    if (contentLength < 0){} 
    else{ 
     byte[] data = new byte[512]; 
     int len = 0; 
     try{ 
      while (-1 != (len = inputStream.read(data))) 
      { 
       buffer.append(new String(data, 0, len)); //converting to string and appending to stringbuffer 
      } 
      }catch (IOException e){e.printStackTrace();} 
     try{ 
      inputStream.close(); // closing the stream 
      }catch (IOException e){e.printStackTrace();} 

    res = buffer.toString();  // converting stringbuffer to string 
    Toast.makeText(UploaddemoActivity.this, "Result : " + res, Toast.LENGTH_LONG).show(); 

    } 
    return res; 
    } 

}

這只是給我的效應初探-1,而不是我在Django發送的字符串

+0

仍然沒有工作... – user1163236 2012-02-09 02:36:58

回答

0

嘗試

public String convertResponseToString(HttpResponse response){ 
    return EntityUtils.toString(response.getEntity(); 
} 
+0

嘗試,但仍無法正常工作給予我的內容長度-1 – user1163236 2012-02-08 10:28:50

+0

任何人都可以幫助我請.... ....如何解析json對象或字符串在Android中傳遞從Django ??????嘗試了很多東西,但仍然沒有得到 – user1163236 2012-02-20 04:28:43