Android 将数据发布到webservice时出错
我想使用JSON发布数据。但我无法做到这一点。 这是我的java代码:Android 将数据发布到webservice时出错,android,json,android-asynctask,httpresponse,Android,Json,Android Asynctask,Httpresponse,我想使用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.HttpClien
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);
}
}
}
package.com.bandapp;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.util.EntityUtils;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入org.json.JSONTokener;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.widget.ListAdapter;
导入android.widget.simpledapter;
导入android.widget.Toast;
公共类UpcomingShow扩展了ListActivity{
公共静态最终字符串标记\u SHOW\u TITLE=“SHOW\u TITLE”;
公共静态最终字符串TAG\u SHOW\u vention=“SHOW\u vention”;
公共静态最终字符串标记\u SHOW\u DATE=“SHOW\u DATE”;
公共静态最终字符串标记\u SHOW\u TIME=“SHOW\u TIME”;
公共静态字符串URL=”http://example.com/example/example/mainAPI.php";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.Uncoming_show);
新建AsyncData().execute();
}
类AsyncData扩展了AsyncTask{
JSONParser-jParser;
ArrayList upcomingShows;
ProgressDialog;
@凌驾
受保护的void onPreExecute(){
pDialog=newprogressdialog(UpcomingShow.this);
pDialog.setTitle(“加载…”);
setMessage(“请稍候…”);
pDialog.show();
super.onPreExecute();
}
@凌驾
受保护的Void doInBackground(字符串…args){
//TODO自动生成的方法存根
jParser=newjsonparser();
List params=new ArrayList();
upcomingShows=新建ArrayList();
add(新的BasicNameValuePair(“rquest”,“={”);
参数添加(新的BasicNameValuePair(“方法”、“波段信息”);
add(新的BasicNameValuePair(“body”,“[{}]}”);
字符串res=“”;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(URL);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=httpclient.execute(httppost);
res=EntityUtils.toString(response.getEntity());
JSONTokener t=新的JSONTokener(res);
JSONArray a=新JSONArray(t);
JSONObject o=a.getJSONObject(0);
String sc=o.getString(标记显示标题);
如果(sc等于(“1”))
{
//成功发布
Toast.makeText(UpcomingShow.this,sc,Toast.LENGTH_SHORT).show();
}
其他的
{
//发生错误
Toast.makeText(UpcomingShow.this,“Fail.”,Toast.LENGTH_SHORT.show();
}
}
捕获(例外e)
{
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
if(pDialog!=null&&pDialog.isShowing()){
pDialog.disclose();
}
ListAdapter=new SimpleAdapter(UpcomingShow.this,upcomingShows,R.layout.upcomingShows_行,新字符串[]){
TAG_SHOW_TITLE,TAG_SHOW_DATE,TAG_SHOW_TIME,TAG_SHOW_vention},新int[]{R.id.textTitle,R.id.textdate,
R.id.textTime,R.id.textVincement});
setListAdapter(适配器);
}
}
}
此外,我无法为我保存在doInBackground()中的任何消息敬酒。你能帮我解决这个问题吗
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
您不能在后台的do上更新UI,您可以在onPostExecute
中获得result
,并能够弹出这些toast
删除此代码表单doInBackground
您不能在后台的do上更新UI,您可以在
onPostExecute
中获得result
,并能够弹出这些toast 您不能进入doInBackground()
,因为您不能在线程执行期间更新UIview!您应该使用'onProgress'
和'publishProgress'
更改:
class AsyncData extends AsyncTask<String, Void, Void>
然后进入doInBackground():
您不能进入
doInBackground()
,因为您不能在线程执行期间更新UIview!您应该使用'onProgress'
和'publishProgress'
更改:
class AsyncData extends AsyncTask<String, Void, Void>
然后进入doInBackground():
在执行
AsyncTask
任务时,您无法在主线程、用户Log.d(“标记”、“您的文本”)中实现Toast显示
您可以在onPostExecution()中实现Toast
}
在执行
AsyncTask
任务时,您无法在主线程、用户Log.d(“标记”、“您的文本”)中实现Toast显示
您可以在onPostExecution()中实现Toast
}
我试着通过邮递员(谷歌扩展)发送一个帖子,你提供的URL以HTTP状态200响应,但没有响应消息。问题是,根据提供的代码,您希望收到来自上述url的消息响应。您可能应该与正在连接的服务器联系。我尝试通过Postman(谷歌扩展)发送您的请求,您提供的URL以HTTP状态200响应,但没有响应
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
if (values[0] != null)
Toast.makeText(UpcomingShow.this, values[0], Toast.LENGTH_SHORT).show();
}
if(sc.equals("1"))
{
publishProgress(sc);
}
else
{
publishProgress("Fail.");
}
}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();
}
}
}