在下面我发送请求到服务器,其中应用程序由django处理。我的问题是来自django如何将响应发送回Android客户端..
django代码:
def mylogin(request):
// return HttpResponse or render_to_response ....
这是我的安卓代码
package senddata.com;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SenddataActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "data sent", Toast.LENGTH_LONG).show();
try
{
Toast.makeText(getApplicationContext(), "data sent1", Toast.LENGTH_LONG).show();
HttpClient client = new HttpClient();
String postURL="http://192.171.254.61/mylogin/";
HttpPost post = new HttpPost(postURL);
MultipartEntity reqEntity = new MultipartEntity();
//send data
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String r = EntityUtils.toString(resEntity);
if(r.startsWith("success")) {
Toast.makeText(getApplicationContext(), "data sent", Toast.LENGTH_LONG).show();
}
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "data not sent" + e, Toast.LENGTH_LONG).show();
}
}
});
}
}