如何将数据发送到采用了android通过HTTP POST方法服务器? [英] how to send data to server using android by Http post method?

查看:225
本文介绍了如何将数据发送到采用了android通过HTTP POST方法服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用Android手机的数据发送到我的服务器POST方法

I am trying to send data from android phone to my server using post method

服务器:写在JSP
 数据库:JDO

server: written in JSP database : JDO

时,如图所示,code为HTTP POST,JSP文件和java code

the code for http post,jsp file and java code is as shown

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button sig = (Button)findViewById(R.id.button1);
     sig.setOnClickListener(this);
    tv = (TextView)findViewById(R.id.textView1);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
     HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://egencies.appspot.com");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
            nameValuePairs.add(new BasicNameValuePair("Rid", "00-22-68-E8-EC-F1"));
            nameValuePairs.add(new BasicNameValuePair("location", "bangalore"));
            nameValuePairs.add(new BasicNameValuePair("content", "hello frm android"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            tv.setText(response.toString());

        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        }

JSP文件表单标签:

jsp file Form tag:

<form action="/sign" method="get">
<div><input type="text" name="Rid" /></div>
<div><input type="text" name="location" /></div>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Post Greeting" /></div>
<input type="hidden" name="guestbookName" value="<%= guestbookName %>"/>
</form>

Java的code对JDO:

java code for JDO:

String guestbookName = req.getParameter("guestbookName");
Key guestbookKey = KeyFactory.createKey("Guestbook", guestbookName);
String content = req.getParameter("content");
String Rid = req.getParameter("Rid");
String location = req.getParameter("location");
Date date = new Date();
Entity greeting = new Entity("Greeting", guestbookKey);
greeting.setProperty("user", user);
greeting.setProperty("date", date);
greeting.setProperty("content", content);
greeting.setProperty("Rid", Rid);
greeting.setProperty("location", location);
DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();
datastore.put(greeting);

resp.sendRedirect("/guestbook.jsp?guestbookName="
             + guestbookName);"

当我把它我收到味精说org.apache.http.message.BasicHtt$p$psponse@44f94aa0
但没有被得到存储在数据库中。
谁能帮助我?

when i send it i receive msg saying org.apache.http.message.BasicHttpResponse@44f94aa0 but nothing is getting stored in the database. can anyone help me with this?

推荐答案

是否本身的形式很好地工作?该表单使用GET,POST不,和我想象你的java code对JDO正在寻求获取数据,不自检。因此,要么修改窗体和Java code对JDO使用POST或修改了Android code使用GET。

Does the form by itself work fine? The form is using GET, not POST, and I imagine your java code for JDO is looking at GET data, not POST. So either modify the form and the java code for JDO to use POST or modify the Android code to use GET.

这篇关于如何将数据发送到采用了android通过HTTP POST方法服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆