【问题标题】:android XML send and get receive via HTTP postandroid XML通过HTTP post发送和接收
【发布时间】:2013-03-12 07:14:08
【问题描述】:

首先,我尝试能够发送带有以下参数的 HttpResponse:

http://www.syslang.com/frengly/controller?action=translateREST&src=en&dest=iw&text=good&email=YYY&password=XXX

代码如下所示:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.syslang.com/frengly/controller");   
List<NameValuePair> pairs = new ArrayList<NameValuePair>();  
pairs.add(new BasicNameValuePair("src", "en")); 
pairs.add(new BasicNameValuePair("dest", "iw"));  
pairs.add(new BasicNameValuePair("text", "good"));                  
pairs.add(new BasicNameValuePair("email", "YYY")); 
pairs.add(new BasicNameValuePair("password", "XXX"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs,HTTP.UTF_8);  
httpPost.setEntity(entity);  
HttpResponse response = httpClient.execute(httpPost); 
HttpEntity httpEntity = response.getEntity();

API structure is available at http://www.frengly.com/ (under the API tab) 共有 5 个参数(src、dest、text、email、password)。

到目前为止,我每次尝试拨打电话

HttpResponse 响应 = httpClient.execute(httpPost); 我不断收到 IO 异常 :(

之后我应该得到类似这样的结构:

-<root>
  <text>good</text>
  <translation>טוב</translation>
  <translationFramed>טוב|</translationFramed>
  <missing/>
  <existing>good,</existing>
  <stat>1/1</stat>
</root>

我想我会处理这部分来构建 XML 并根据需要解析它

ps:我查过了 Android, send and receive XML via HTTP POST method 和许多其他链接,这对我没有多大帮助。

如果需要我的应用程序中的任何代码行,请告诉我...

提前致谢。

【问题讨论】:

    标签: android xml api xmlhttprequest http-post


    【解决方案1】:

    您不需要发布。 您需要改为 GET。

    // Construct your request here.
    String requestURL= "http://www.syslang.com/frengly/controller?action=translateREST&src=en&dest=iw&text=good&email=YYY&password=XXX"
    
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(requestURL);
    HttpResponse response = httpClient.execute(httpGet);
    

    还有你的 AndroidManifest.xml 中的互联网权限

    <uses-permission android:name="android.permission.INTERNET"/>
    <application
    ....
    ....
    

    【讨论】:

    • 感谢您的快速响应,但这仍然无法正常工作。在“HttpResponse response = httpClient.execute(httpGet);”行上引发了相同的 IO 异常行:(
    • 你添加了上网权限吗?
    • 哦,也许我没有
    • 非常感谢,伙计,我确定你帮我省了很多麻烦是多么有用……再次感谢 :)
    • 没问题。如果它解决了您的问题,请接受此作为答案。 :)
    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 2011-02-18
    • 2014-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多