我已經創建了一個android應用程序,用於將JSON對象從我的應用程序發送到Web服務。將JSON對象發送到Android中的Web服務會創建重複的JSON對象
我可以將數據發送到Web服務。
如何解決問題,即web服務顯示重複 JSON對象?
這裏是我的代碼:
// Vehicle Condition Details taken Activity
public class VechileConditionActivity extends Activity {
EditText Kms,Fuel,Regno,Licence;
RadioGroup oilRadioGroup,tyreRadioGroup,treadRadioGroup;
String Mileage,Fuellevel,Oillevels,Pressure,deapth,Vehiclenumber,Licencenumber;
private ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.vechilecondition1);
Kms = (EditText)findViewById(R.id.editText1);
Fuel = (EditText)findViewById(R.id.editText2);
Regno = (EditText)findViewById(R.id.editText3);
Licence = (EditText)findViewById(R.id.editText4);
//Taking action bar control
actionBar = getActionBar();
// Set the action bar properties includes Title,Icon,Background
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(getApplicationContext().getResources().getDrawable(R.drawable.vehiclecondtion));
actionBar.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.titlebar));
// Set Bottom Action bar with background image
actionBar.setSplitBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.footer));
oilRadioGroup =(RadioGroup) findViewById(R.id.radioGroup1);
oilRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
RadioButton radioButton = (RadioButton) findViewById(arg1);
Oillevels = radioButton.getText().toString();
}
});
tyreRadioGroup =(RadioGroup) findViewById(R.id.radioGroup3);
tyreRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
RadioButton radioButton = (RadioButton) findViewById(arg1);
Pressure = radioButton.getText().toString();
}
});
treadRadioGroup =(RadioGroup) findViewById(R.id.radioGroup2);
treadRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
RadioButton radioButton = (RadioButton) findViewById(arg1);
deapth = radioButton.getText().toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
//onOptionsItemSelected is called whenever an item in your options menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
// case to upload the vehicle condition details
case (R.id.itemid_0):
new sendJson().start();
Toast.makeText(getApplicationContext(), "uploading the information", Toast.LENGTH_LONG).show();
break;
// case to capture the vehicle
case (R.id.itemid_1):
Bundle bundle = getIntent().getExtras();
//Extract the data…
String Driverid = bundle.getString("Driverid");
Toast.makeText(getApplicationContext(), "capturing details of drivercode "+Driverid,Toast.LENGTH_LONG).show();
break;
// case to move to home screen to download the delivery list
case (R.id.itemid_2):
Intent intent = new Intent();
intent.setClass(getApplicationContext(), HomeScreenActivity1.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
public class sendJson extends Thread
{
// declared locally so that it destroys after serving its purpose
JSONObject jsonobj = new JSONObject();
public void run() {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://XXXXX/XXXXXX");
Looper.prepare(); //For Preparing Message Pool for the child Thread
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
// adding some keys
Bundle bundle = getIntent().getExtras();
//Extract the data…
String Driverid = bundle.getString("Driverid");//this is for String
String Mileage = Kms.getText().toString();
String Fuellevel = Fuel.getText().toString();
String Vehiclenumber = Regno.getText().toString();
String Licencenumber = Licence.getText().toString();
String Oil = Oillevels;
String Air = Pressure;
String tread = deapth;
jsonobj.put("DriverCode", Driverid);
jsonobj.put("MileageReading",Mileage);
jsonobj.put("FuelLevel",Fuellevel);
jsonobj.put("VechileNum",Vehiclenumber);
jsonobj.put("LicenceNum",Licencenumber);
jsonobj.put("OilLevel",Oil);
jsonobj.put("TyrePressure",Air);
jsonobj.put("ThredDepth",tread);
Log.i("coooooooooooooooooooooooooooooooooooooood", "DriverCode:"+Driverid);
Log.i("coooooooooooooooooooooooooooooooooooooood", "MileageReading:"+Mileage);
Log.i("coooooooooooooooooooooooooooooooooooooood", "FuelLevel:"+Fuellevel);
Log.i("coooooooooooooooooooooooooooooooooooooood", "VechileNum:"+Vehiclenumber);
Log.i("coooooooooooooooooooooooooooooooooooooood", "LicenceNum:"+Licencenumber);
Log.i("coooooooooooooooooooooooooooooooooooooood", "OilLevel:"+Oil);
Log.i("coooooooooooooooooooooooooooooooooooooood", "TyrePressure:"+Air);
Log.i("coooooooooooooooooooooooooooooooooooooood", "ThredDepth:"+tread);
StringEntity se;
try {
se = new StringEntity(jsonobj.toString());
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
HttpResponse response = client.execute(post);
InputStream inputStream = response.getEntity().getContent();
HttpResponse httpresponse = client.execute(post, new BasicHttpContext()); //For thread safe
String response1 = EntityUtils.toString(httpresponse.getEntity());
response1 = response1.replaceAll("\\\"", "\"");
Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1);
Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JSONException ex) {
//buildref.setText("Error Occurred while building JSON");
ex.printStackTrace();
}
}
}
什麼,你做過調試這個問題? – 2014-09-23 19:53:16