2012-07-10 72 views
1

我可以使用HTTP post和response檢索xml文件。但是,現在我需要發佈String參數以及URL。下面的代碼告訴我,HTTP staus代碼是不正確的(即500),所以它返回null,然後我得到一個NullPointerException如何將參數發佈到Android的Web服務?

package com.JobsWebService; 
import java.io.IOException; 
import java.io.Reader; 
import java.io.StringReader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
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.simpleframework.xml.Serializer; 
import org.simpleframework.xml.core.Persister; 
import android.util.Log; 

public class XmlConnection { 

private static final String url =   "http://www.accuservlite.com/AccumobileWS/TestService.asmx/RyanMB_GetJobs"; 
private DefaultHttpClient client = new DefaultHttpClient(); 
String param= "Johnny"; 

public List<NewJob> RyanMB_GetJobs() { 
    try { 
     String xmlData = retrieve(url); 
     Serializer serializer = new Persister(); 
     Reader reader = new StringReader(xmlData); 
     ArrayOfNewJob testService = serializer.read(ArrayOfNewJob.class, 
       reader, false); 
     Log.i("gary", "Worked"); 
     return testService.NewJob; 

    } catch (Exception e) { 
     Log.i("gary", e.toString()); 
    } 
    return null; 
} 

// method retrieve 
public String retrieve(String url) throws UnsupportedEncodingException { 

    List<NameValuePair> qparams = new ArrayList<NameValuePair>(); 
    qparams.add(new BasicNameValuePair("Johnny", "Johhny")); 

    UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(qparams, 
      HTTP.UTF_8); 

    HttpPost getRequest = new HttpPost(url); 

    getRequest.setEntity(postEntity); 

    try { 

     HttpResponse getResponse = client.execute(getRequest); 

     final int statusCode = getResponse.getStatusLine().getStatusCode(); 

     if (statusCode != HttpStatus.SC_OK) { 

      return null; 
     } 

     HttpEntity getResponseEntity = getResponse.getEntity(); 

     if (getResponseEntity != null) { 
      return EntityUtils.toString(getResponseEntity); 
     } 

    } catch (IOException e) { 
     Log.i("gary", "Error for URL " + url, e); 
     getRequest.abort(); 
    } 
    return null; 
} 

}

回答

0

500服務器錯誤,並沒有給我的印象顯然是錯誤的,此代碼。您是否根據發佈請求測試了服務器?服務器是否在給它一個名稱值對時期待一個字符串?

您可以嘗試使用Wfetch進行測試。

+0

此外,還有另一種有用的工具用於生成HTTP GET/POST/PUT/etc請求到服務器,我使用。它是[WizTools.org RESTClient](http://rest-client.googlecode.com/)。它是用java編寫的,與平臺無關。 – Prizoff 2012-07-10 17:33:55

0

要YOUT HttpPost對象添加POST參數,您將使用NameValuPair的列表:

List<NameValuePair> parameters = new ArrayList<NameValuePair>(); 
parameters.add(new BasicNameValuePair("name_param1", "value_param1")); 

而且這個參數添加到您的HttpPost:

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); 
httpPost.setEntity(formEntity); 
+0

謝謝,這工作!我有「name_param1」錯誤。 – 2012-07-11 11:35:06

相關問題