how to send data to server using android by Http p

2019-08-28 06:43发布

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

server: written in JSP database : JDO

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 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 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);"

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?

1条回答
做个烂人
2楼-- · 2019-08-28 07:03

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.

查看更多
登录 后发表回答