package com.fff; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.output.XMLOutputter; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class JOM_POST_Test1Activity extends Activity { /** Called when the activity is first created. */ String aa=""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { BuildXMLDoc(); } catch (Exception e) { e.printStackTrace(); } final String TAG = "ApacheHttpClientTest"; String response = ""; String url = "http://10.40.204.137:8080/project/test"; Listparams = new ArrayList (); params.add(new BasicNameValuePair("id",aa)); params.add(new BasicNameValuePair("name","kuka")); try { response = httpPost(url, params); } catch (Exception e) { e.printStackTrace(); } Log.i(TAG, "####"+response); } public void BuildXMLDoc() throws IOException, JDOMException { List list = new ArrayList (); List list2 = new ArrayList (); list.add(0, "1111111111"); list2.add(0, "11,22,33,44,55"); list.add(1, "2222222222"); list2.add(1, "11,22,33,44,55"); list.add(2, "3333333333"); list2.add(2, "11,22,33,44,55"); Element root = new Element("list"); Document Doc = new Document(root); Element elements; XMLOutputter outputter = new XMLOutputter(); for(小於list容量時) { elements= new Element("user"); elements.setAttribute("id", "" + i); elements.addContent(new Element("_id").setText(list.get(i))); elements.addContent(new Element("oj_want").setText(list2.get(i))); root.addContent(elements); aa+= outputter.outputString(elements); } System.out.println(aa); /* //如要產生XML的檔案使用以下 XMLOutputter XMLOut = new XMLOutputter(); XMLOut.output(Doc, new FileOutputStream("user.xml")); */ } public String httpPost(String url, List params) throws Exception { String response = null; HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try { httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); HttpResponse httpResponse = httpclient.execute(httppost); int statusCode = httpResponse.getStatusLine().getStatusCode(); if(statusCode==HttpStatus.SC_OK) { response = EntityUtils.toString(httpResponse.getEntity()); } else { response = "錯誤"+statusCode; } }catch (Exception e) { e.printStackTrace(); } return response; } }
2012年4月23日 星期一
JDOM搭配HttpClient Post
今天要把Android上的某字串以XML的方式POST給Server,使用了JDOM(要先下載\並匯入.jar檔)將訊息建立成XML格式後存入字串變數,再以Post的方式給Server,如以下:
訂閱:
張貼留言 (Atom)
星期、月份英文縮寫
中文 英文 3字母 縮寫 3字母 縮寫 2字母 縮寫 2字母 縮寫 1字母 縮寫 1字母 縮寫 其他 縮寫 星期日 Sunday SUN Sun SU Su S S 星期一 Monday MON Mon MO Mo M M 星期二 Tuesday TUE Tue TU ...
-
今天想做個Edittext輸入自動搜尋,原本使用setOnKeyListener的時候,輸入中文都要再按一次完成才會即時才會擷取,結果發現 addTextChangedListener 更好用呢,一輸入就即時更新字串,跟WhatsApp的搜尋聯絡人一樣,很即時搜尋唷。 ...
-
要把經緯度店址可以使用以下作法 Geocoder gc = new Geocoder(getActivity(), Locale.TRADITIONAL_CHINESE); List<Address> lstAddress = gc.getFromLocation...
-
中文 英文 3字母 縮寫 3字母 縮寫 2字母 縮寫 2字母 縮寫 1字母 縮寫 1字母 縮寫 其他 縮寫 星期日 Sunday SUN Sun SU Su S S 星期一 Monday MON Mon MO Mo M M 星期二 Tuesday TUE Tue TU ...
沒有留言:
張貼留言