2012-03-06 135 views
0

我想將數據保存到服務器中。這是我簡單的PHP代碼:從android向服務器插入數據

<?php 
require('Zend\Db.php'); 
require('Zend\Db\Exception.php'); 
function sendResponse($status, $payload) 
{ 
$responseCodeList = array(
    200 => 'OK', 
    201 => 'Created', 
    204 => 'No Content', 
    301 => 'Moved Permanently', 
    400 => 'Bad Request', 
    404 => 'Not Found', 
    410 => 'Gone', 
    501 => 'Not Implemented', 
); 

header('HTTP/1.1 ' . $status . ' ' . $responseCodeList[$status]); 
header('Content-Type: text/plain'); 
echo (string) $payload; 
} 
try { 

$db = Zend_Db::factory('Oracle', array(
    'host'  => '10.234.152.6', 
    'username' => 'DMHR', 
    'password' => '', 
    'dbname'  => '//10.234.152.6/SATHRIS', 
)); 
} catch (Zend_Db_Exception $e) { 
echo sendResponse(200,'100|Database Error'); 
exit; 
} 

$method = $_SERVER['REQUEST_METHOD']; 

$payload = ''; 

if ($method = 'GET') { 
    $data = $_GET; 
try { 
    $array_data = array(
     'AD_KODE_TOKO' => $data['kode_toko'], 
     'AD_NAMA' => $data['nama']; 
     'AD_ALAMAT' => $data['alamat'], 
     'AD_KODE_POS' => $data['kode_pos'], 
     'AD_NO_TELP' => $data['no_telp'], 
     'AD_LONGITUDE' => $data['longitude'], 
     'AD_LATITUDE' => $data['latitude'], 
     'AD_PERUSAHAAN' => $data['perusahaan'], 
    ); 

    $db->beginTransaction(); 
    $db->insert('ANDROID_DATA', $array_data); 
    $db->commit(); 
    $payload = 'SUCCESS'; 
} catch(Zend_Db_Exception $e) { 
    $payload = 'FAILURE'; 
    echo "<pre>"; 
    print_r($e); 
    echo "</pre>"; 
} 

} 

echo sendResponse(200, $payload); 

...這是我保存到服務器主要活動:

private EditText etkdstore; 
private EditText etnama; 
private EditText etalamat; 
private EditText etkdpos; 
private EditText etnotelp; 
private EditText etlng; 
private EditText etlat; 
private EditText etperush; 
//private TextView idgambar; 

private Button save; 

private String url = "10.234.152.6/upload_get.php"; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    etkdstore = (EditText)findViewById(R.id.kdstore); 
    etnama = (EditText)findViewById(R.id.nama); 
    etalamat = (EditText)findViewById(R.id.alamat); 
    etkdpos = (EditText)findViewById(R.id.kdpos); 
    etnotelp = (EditText)findViewById(R.id.telp); 
    etlng = (EditText)findViewById(R.id.lng); 
    etlat = (EditText)findViewById(R.id.lat); 
    etperush = (EditText)findViewById(R.id.perush); 
    // idgambar = (TextView)findViewById(R.id.gambar); 

    save = (Button)findViewById(R.id.save); 

    save.setOnClickListener(new Button.OnClickListener() { 
      //@Override 
    public void onClick(View v) { 
    try { 
    // setiap parameter yang akan dikirim melalui http 
    // harus encode agar 
    // dapat terbaca dengan baik oleh server 
    String kdstore = URLEncoder.encode(etkdstore.getText().toString(), "utf-8"); 
    String nama = URLEncoder.encode(etnama.getText().toString(), "utf-8"); 
    String alamat = URLEncoder.encode(etalamat.getText().toString(), "utf-8"); 
    String kdpos = URLEncoder.encode(etkdpos.getText().toString(), "utf-8"); 
    String notelp = URLEncoder.encode(etnotelp.getText().toString(), "utf-8"); 
    String lng = URLEncoder.encode(etlng.getText().toString(), "utf-8"); 
    String lat = URLEncoder.encode(etlat.getText().toString(), "utf-8"); 
    String perush = URLEncoder.encode(etperush.getText().toString(), "utf-8"); 
    //String gambar = URLEncoder.encode(idgambar.getText().toString(), "utf-8"); 
    url += "?kode_toko=" + kdstore + "&&nama" + nama + "&&alamat=" + alamat + "&&kode_pos=" + kdpos + 
    "&&no_telp=" + notelp + "&&longitude=" + lng + "&&latitude=" + lat + "&&perusahaan=" + perush; 
    getRequest(url); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     } 
     } 
    }); 
} 
public void getRequest(String Url) { 

      Toast.makeText(this, "Tambah Data " + Url + " ", Toast.LENGTH_SHORT).show(); 
      HttpClient client = new DefaultHttpClient(); 
      HttpGet request = new HttpGet(url); 
      try { 
       HttpResponse response = client.execute(request); 
       Toast.makeText(this, "Tambah Data " + request(response) + " ",Toast.LENGTH_SHORT).show(); 
      } catch (Exception ex) { 
       Toast.makeText(this, "Tambah Data Gagal !", Toast.LENGTH_SHORT).show(); 
      } 
     } 
public static String request(HttpResponse response) { 

      String result = ""; 
      try { 
       InputStream in = response.getEntity().getContent(); 
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(in)); 
       StringBuilder str = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        str.append(line + "\n"); 
       } 
       in.close(); 
       result = str.toString(); 
      } catch (Exception ex) { 
       result = "Error"; 
      } 

      return result; 
     } 

在我的情況,我想將數據保存到服務器,但我無法將數據保存到服務器中。誰能幫我?

回答

1

看到這個example.I認爲它可以幫助你

  JSONObject json = new JSONObject(); 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://www.mernalaw.com/financeassessment.php"); 
      json.put("Income","1 - $30,000"); 
      json.put("Household","1"); 

      json.put("Employment","Employed"); 
      json.put("RealEstate","Primary residence"); 
      json.put("Vehicle","No"); 
      json.put("Name","vvk"); 
      json.put("Email","[email protected]"); 
      json.put("CellPhone","99999999999"); 
      json.put("City","hyd"); 

      Log.i("jason Object", json.toString()); 
      httppost.setHeader("json", json.toString()); 

      StringEntity se = new StringEntity(json.toString()); 
      se.setContentEncoding("UTF-8"); 
      se.setContentType("application/json"); 
      httppost.setEntity(se);    
      HttpResponse response = httpclient.execute(httppost); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      Toast.makeText(getApplicationContext(),String.valueOf(statusCode), Toast.LENGTH_SHORT).show(); 
      is = se.getContent(); 
      Log.e("log_tag", "connection success "+"status code"+statusCode); 
      Toast.makeText(getApplicationContext(), "Successfully Connected", Toast.LENGTH_SHORT).show(); 
      } 
      catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
      Toast.makeText(getApplicationContext(), "Fail to Connect", Toast.LENGTH_SHORT).show(); 
      } 
+0

是謝謝!!我的問題解決了:) – akubabas 2012-03-06 07:54:18

+0

現在我想要上傳的SD卡文件到目錄上的Web server..how上傳進入目錄在web服務器??謝謝 – akubabas 2012-03-06 08:07:08

+0

使用此鏈接http://stackoverflow.com/questions/9272518/upload-imageview-to-server/9272639#9272639 – 2012-03-06 09:49:21

相關問題