0

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?

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
Rahul
  • 67
  • 2
  • 8
  • Please explain to me where JDO is in this post ? DatastoreService and its factory is not JDO, and is presumably GAE – DataNucleus Mar 14 '12 at 14:32
  • ya i am using datastore and presistancemanager , it works as JDO right?? or i am wrong?? – Rahul Mar 14 '12 at 15:48
  • Where is PersistenceManager ? You use DatastoreService.put in your code ... that is not JDO. – DataNucleus Mar 14 '12 at 15:55
  • Can you change tv.setText(response.toString()); to tv.setText(response.getStatusLine().getStatusCode()); and tell us what you've got? – Martin Gamulin Mar 14 '12 at 17:35
  • when i change to tv.setText(response.getStatusLine().getStatusCode()); i get force close....but with tv.setText(response.toString()) it gives response as i said above – Rahul Mar 15 '12 at 06:20

1 Answers1

0

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.

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • oops....i have updated the code to Post method forgot to change here...yup form works fine you can check on http://egencies.appspot.com – Rahul Mar 14 '12 at 15:46