2012-03-14 109 views
0

我試圖用POST方法如何使用android通過Http post方法發送數據到服務器?

服務器從Android手機發送數據到我的服務器:寫在JSP 數據庫:JDO

爲HTTP POST,JSP文件和Java代碼的代碼所示

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文件表單標籤:對JDO

<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代碼:

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

當我把它我收到味精說[email protected] 但沒有得到被存儲在數據庫中。 任何人都可以幫助我嗎?

+0

請向我解釋JDO在這篇文章中的位置? DatastoreService和它的工廠不是JDO,並且估計GAE – DataNucleus 2012-03-14 14:32:59

+0

你正在使用數據存儲和presistancemanager,它的工作原理是JDO嗎?或者我錯了? – Rahul 2012-03-14 15:48:04

+0

PersistenceManager在哪裏?你在你的代碼中使用DatastoreService.put ...這不是JDO。 – DataNucleus 2012-03-14 15:55:13

回答

0

形式本身是否正常工作?表單使用GET,而不是POST,我想象你的JDO的java代碼正在查看GET數據,而不是POST。因此,修改表單和JDO的Java代碼以使用POST或修改Android代碼以使用GET。

+0

哎呀....我已經更新了代碼Post方法忘記在這裏改變... yup表單工作正常,你可以檢查http://egencies.appspot.com – Rahul 2012-03-14 15:46:08

相關問題