2013-04-24 80 views
0

我想使用JSON發佈數據。但我無法做到這一點。 這是我的Java代碼:將數據發佈到webservice時發生錯誤

package com.bandapp; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import org.apache.http.HttpResponse; 
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.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import org.json.JSONTokener; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.ListAdapter; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 

public class UpcomingShow extends ListActivity { 
    public static final String TAG_SHOW_TITLE = "show_title"; 
    public static final String TAG_SHOW_VENUE = "show_venue"; 
    public static final String TAG_SHOW_DATE = "show_date"; 
    public static final String TAG_SHOW_TIME = "show_time"; 
    public static String URL = "http://example.com/example/example/mainAPI.php"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.upcoming_show); 
     new AsyncData().execute(); 
    } 

    class AsyncData extends AsyncTask<String, Void, Void> { 
     JSONParser jParser; 
     ArrayList<HashMap<String, String>> upcomingShows; 
     ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      pDialog = new ProgressDialog(UpcomingShow.this); 
      pDialog.setTitle("Loading...."); 
      pDialog.setMessage("Please wait..."); 
      pDialog.show(); 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(String... args) { 
      // TODO Auto-generated method stub 
      jParser = new JSONParser(); 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      upcomingShows = new ArrayList<HashMap<String,String>>(); 
      params.add(new BasicNameValuePair("rquest", "={")); 
      params.add(new BasicNameValuePair("method","band_info")); 
      params.add(new BasicNameValuePair("body","[{}]}")); 

      String res = ""; 
      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(URL); 
       httppost.setEntity(new UrlEncodedFormEntity(params)); 
       HttpResponse response = httpclient.execute(httppost); 
       res = EntityUtils.toString(response.getEntity()); 
       JSONTokener t = new JSONTokener(res); 
       JSONArray a = new JSONArray(t); 
       JSONObject o = a.getJSONObject(0); 
       String sc = o.getString(TAG_SHOW_TITLE); 
       if(sc.equals("1")) 
       { 
        // posted successfully 
        Toast.makeText(UpcomingShow.this, sc, Toast.LENGTH_SHORT).show(); 
       } 
       else 
       { 
       // error occurred 
        Toast.makeText(UpcomingShow.this, "Fail.", Toast.LENGTH_SHORT).show(); 
       } 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      if (pDialog != null && pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } 
      ListAdapter adapter = new SimpleAdapter(UpcomingShow.this, upcomingShows, R.layout.upcomingshows_row, new String[] { 
        TAG_SHOW_TITLE, TAG_SHOW_DATE, TAG_SHOW_TIME, TAG_SHOW_VENUE }, new int[] { R.id.textTitle, R.id.textdate, 
        R.id.textTime, R.id.textVenue }); 
      setListAdapter(adapter); 
     } 
    } 
} 

而且我不能夠敬酒任何我一直在doInBackground(消息)。你可以幫我解決這個請...

+0

http://clients.etilox.com/kaialece/web_service/mainAPI.php鏈接不工作..在這裏輸入您的日誌.. – 2013-04-24 09:59:13

+1

是的,因爲它只滿足POST請求... – 2013-04-24 10:02:10

回答

1

因爲無法在線程執行期間更新UIview,所以不能敬酒到doInBackground()!你應該使用​​和'publishProgress'

變化:

class AsyncData extends AsyncTask<String, Void, Void> 

到:

class AsyncData extends AsyncTask<String, String, Void> 

並重寫onProgress吐司:

@Override 
protected void onProgressUpdate(String... values) { 
    super.onProgressUpdate(values); 
     if (values[0] != null) 
       Toast.makeText(UpcomingShow.this, values[0], Toast.LENGTH_SHORT).show(); 
} 

而進入doInBackground():

if(sc.equals("1")) 
{ 
    publishProgress(sc); 
    } 
    else 
    { 
    publishProgress("Fail."); 
} 
1
if(sc.equals("1")) 
       { 
        // posted successfully 
        Toast.makeText(UpcomingShow.this, sc, Toast.LENGTH_SHORT).show(); 
       } 
       else 
       { 
       // error occurred 
        Toast.makeText(UpcomingShow.this, "Fail.", Toast.LENGTH_SHORT).show(); 
       } 

中刪除此代碼形式doInBackground

你不能更新在後臺做UI,您可以在onPostExecute,並能夠得到result彈出這些吐司。

1

在做AsyncTask<String, Void, Void>任務,你不能實現在主線程,用戶Log.d(「標記」,「你的文本」)吐司顯示;

您可以onPostExecution()實現吐司

}catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 

      return sc; 

}

protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      if (pDialog != null && pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } 
     if(result.equals("1")) 
      { 
       // posted successfully 
       Toast.makeText(UpcomingShow.this, result, Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
      // error occurred 
       Toast.makeText(UpcomingShow.this, "Fail.", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 
1

我試圖通過郵差(谷歌推廣)和URL發送後您的要求您提供迴應HTTP狀態200但沒有響應消息。問題是,根據提供的代碼,您期望來自該網址的消息響應。您應該檢查您正在連接的服務器。