2013-04-24 76 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但没有响应消息。问题是,根据提供的代码,您期望来自该网址的消息响应。您应该检查您正在连接的服务器。