2014-09-23 44 views
0

我已經創建了一個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(); 
    }  
} 

} 
+0

什麼,你做過調試這個問題? – 2014-09-23 19:53:16

回答

0

你怎麼知道的是,Web服務收到重複的JSON對象?

您是直接查看Web服務還是僅查看應用程序的日誌?

因爲您創建了兩個相同的日誌:

Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1); 
Log.i("coooooooooooooooooooooooooooooooooooooood", "Server Response : " + response1); 
+0

可能應該添加到評論和刪除,因爲它不是一個答案 – brwngrldev 2014-09-23 20:26:43

+0

我知道,但我沒有50聲望點需要添加評論 – MauriF 2014-09-23 20:29:34

+0

我發送Android應用程序的數據到SAP表。在表中我'能夠看到接收重複的對象SAP服務工作正常,但問題是與此代碼 – 2014-09-24 06:00:52

0

了我答案,我在我的代碼所做的錯誤是:

嘗試{ SE =新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); 
} 

這是正確的代碼 嘗試{ SE =新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); 

} 

感謝烏拉圭回合答覆我的問題