我已经创建了一个 Android 应用程序来将 JSON 对象从我的应用程序发送到 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();
}
}
}
最佳答案
得到我的答案,我在代码中犯的错误是:
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);
}
这是正确的代码
try {
se = new StringEntity(jsonobj.toString());
se.setContentType("application/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppostreq.setEntity(se);
HttpResponse httpresponse;
try {
httpresponse = httpclient.execute(httppostreq,new BasicHttpContext());
String response1 = EntityUtils.toString(httpresponse.getEntity());
response1 = response1.replaceAll("\\\"", "\"");
Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1);
//Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1);
}
关于java - 将 JSON 对象发送到 Android 中的 Web 服务会创建重复的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26003521/