我想使用webservice来点击我的按钮后获取数据(button1)不幸地停止了我正在使用json我的调试器没有来onclick视图而我的json没有给出响应。
JSONParser jsonParser = new JSONParser();
private static final String about = "about";
private static String url = "http://www.mydomain/staticinfo";
private static final String TAG_SUCCESS = "id";
private static final String TAG_SUCCESS1="success";
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void action(View v)
{
if(v.getId()==R.id.button1)
new aboutAccess().execute();
else
Toast.makeText(this, "This is demo for smlsolution", Toast.LENGTH_LONG).show();
}
class aboutAccess extends AsyncTask<String, String, String>{
private String id;
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
//String url = "http://10.0.2.2/about.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
String ID="1";
params.add(new BasicNameValuePair("id ","ID"));
JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
Log.d("Create Response", json.toString());
try{
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
flag=0;
String abt=json.getString(about);
Intent i = new Intent(getApplicationContext(),About.class);
i.putExtra("about", abt);
startActivity(i);
}
else{
flag=1;
}
}
catch (JSONException e){
e.printStackTrace();
System.out.println(e.toString());
}
// TODO Auto-generated method stub
return null;
}
protected void onPostExecute(String file_url){
pDialog.dismiss();
if(flag==1)
Toast.makeText(MainActivity.this,"Data Not Found", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
您的应用崩溃了,因为您正在Activity
进行doInBackground()
切换操作。将以下代码从doInBackground()
更改为onPostExecute()
,
Intent i = new Intent(getApplicationContext(),AboutUs.class);
startActivity(i);
finish();
执行以下操作,
protected void onPostExecute(String file_url) {
pDialog.dismiss();
if(flag==1)
Toast.makeText(MainActivity.this,"Please Enter Correct informations",Toast.LENGTH_LONG).show();
else{
Intent i = new Intent(getApplicationContext(),AboutUs.class);
startActivity(i);
finish();
}
}