2014-01-15 121 views
-1

我創建了一個將數據發送到web的android程序。垃圾郵件不能正常工作

這裏是我的代碼:

public void sendToweb() { 
    // get the message from the message text box 
    String msg = systemId1.getText().toString(); 
    // make sure the fields are not empty 
    if (msg.length() > 0) { 
     HttpResponse response = null; 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(
       "http://eservices.rondssolar.com/xbusiness.php"); 

     try { 
      List<NameValuePair> nameValuePairs = new   ArrayList<NameValuePair>(
        2); 
      nameValuePairs.add(new BasicNameValuePair("submit", "Save Tracker Data")); 
      nameValuePairs.add(new BasicNameValuePair("systemId", msg)); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      response = httpclient.execute(httppost); 



      Toast.makeText(getApplicationContext(), 
        "DATA SEND" + response.getStatusLine().toString(), 
        Toast.LENGTH_LONG).show(); 




     } catch (ClientProtocolException e) { 
      Toast.makeText(getApplicationContext(), e.toString(), 
        Toast.LENGTH_LONG).show(); 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      Toast.makeText(getApplicationContext(), e.toString(), 
        Toast.LENGTH_LONG).show(); 
      // TODO Auto-generated catch block 
     } 

      } else { 
     // display message if text fields are empty 
     Toast.makeText(getBaseContext(), "All field are required", 
       Toast.LENGTH_SHORT).show(); 
    } 

} 

我的問題是,我沒有得到任何迴應back.Also我送不可在網上。我覺得我的回答= httpclient.execute數據(httppost );代碼不工作

+1

您從UI線程做到這一點?並沒有得到錯誤?或者你如何使用這種方法?如果有錯誤,請添加logcat。 –

+0

我簡單地從菜單 – user3197627

+0

調用這個方法所以沒有錯誤,什麼都沒有發生? –

回答

0

您可以以JSON格式一樣,發表您的參數:

Thread tSendToweb = new Thread(new Runnable() 

    { 
     @Override 
     public void run() 
     { 

      HttpClient hc = new DefaultHttpClient(); 
      String message; 

      HttpPost p = new HttpPost("http://eservices.rondssolar.com/xbusiness.php"); 
      JSONObject object = new JSONObject(); 
      try 
      { 
       object.put("systemId", systemId); 
      } 
      catch (Exception ex) 
      { 
       ex.printStackTrace(); 
      } 

      message = object.toString(); 

      try 
      { 
       p.setEntity(new StringEntity(message, "UTF8")); 
       p.setHeader("Content-type", "application/json"); 
       hc.execute(p); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 


     } 
    }); 
    tAddLocation.start(); 
+0

謝謝先生,但我怎樣才能使用PHP編寫Web部分? – user3197627