0

我想使用 JSON 发布数据。但我无法做到这一点。这是我的java代码:

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() 中的任何消息敬酒。你能帮我解决这个问题吗...

4

4 回答 4

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

你不能在后台更新你的用户界面,你可以进入resultonPostExecute弹出那些吐司。

于 2013-04-24T10:03:41.900 回答
1

你不能干杯, doInBackground()因为你不能在线程执行期间更新 UIview !你应该使用'onProgress'和'publishProgress'

改变 :

class AsyncData extends AsyncTask<String, Void, Void>

到:

class AsyncData extends AsyncTask<String, String, Void>

并覆盖 toast 的 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.");
 }
于 2013-04-24T10:07:21.817 回答
1

在执行AsyncTask<String, Void, Void>任务时,您无法在主线程中实现 Toast 显示,用户 Log.d(“TAG”,“your-text”);

你可以在 onPostExecution() 中实现 Toast

}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();
            }
        }
    }
于 2013-04-24T10:07:37.367 回答
1

我尝试通过 Postman(谷歌扩展)发送您的请求,您提供的 URL 以 HTTP 状态 200 响应,但没有响应消息。问题是,根据提供的代码,您期望来自所述 url 的消息响应。您可能应该检查您正在连接的服务器。

于 2013-04-24T10:09:28.700 回答