我想将数据保存到服务器中。这是我简单的PHP代码:
<?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;
}
就我而言,我想将数据保存到服务器,但我无法将数据保存到服务器中。任何人都可以帮助我吗?
答案 0 :(得分:1)
看到这个例子。我认为这对你有所帮助
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","vvkrishna1989@gmail.com");
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();
}