从Django的android系统解析字符串 [英] Parsing string in android from django
本文介绍了从Django的android系统解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想检查在Django从机器人发送的字符串
然后将响应从django的发送作为一个字符串回机器人
这code工作当我检查它在我的网页浏览器,但没有对Android应用
这是Django的Django的看法code:
I am just trying to check strings sent from android in django and then a response is sent from django as a string back to android this code is working when i check it on my web browser but not on the android app This is django django view code :
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中从Django中发送的字符串
I m trying to parse the string sent from django in android
我的Android应用code是:
my android app code is :
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发送字符串
this is just giving me reponse -1 and not the string which i sent in django
推荐答案
尝试
public String convertResponseToString(HttpResponse response){
return EntityUtils.toString(response.getEntity();
}
这篇关于从Django的android系统解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文