我已经使用php Zend制作了这样的网络服务
<?php
require('Zend\Db.php');
require('Zend\Db\Exception.php');
require('Zend\Debug.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.35',
'username' => 'REPORT',
'password' => 'r4pt0rsdw',
'dbname' => '//10.234.152.35/SATDW',
));
} catch (Zend_Db_Exception $e) {
echo sendResponse(200,'100|Database Error');
exit;
}
$method = $_SERVER['REQUEST_METHOD'];
$payload = '';
if ($method = 'GET') {
$data = $_GET;
//$sql = "select * from rpt_ms_dc where msd_code = :code";
try {
$select = $db->select()
->from(
'RPT_MS_DC',
array(
'KODE' => 'MSD_CODE',
'NAMA' => 'MSD_DESC',
)
)
->where('MSD_CODE = ?', $data['code']);
$stmt = $select->query();
$result = $stmt->fetchAll();
//return $result;
//$payload = $result[0]['KODE'].$result[0]['NAMA'];
$payload = json_encode($result);
//Zend_Debug::dump($result[0]['KODE'].'||'.$result[0]['NAMA']);exit;
} catch(Zend_Db_Exception $e) {
Zend_Debug::dump($e->getMessage());exit;
}
}
echo sendResponse(200, $payload);
结果如下:
[{“KODE”:“A”,“NAMA”:“DC SERPONG”}]
然后我想在我的编辑文本中检索 DC SERPONG 我怎样才能做到这一点?? 这是我的android代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etKode = (EditText)findViewById(R.id.txtKode);
etNama = (EditText)findViewById(R.id.txtBranch);
save = (Button)findViewById(R.id.get);
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 kode = URLEncoder.encode(etKode.getText().toString(), "utf-8");
//String nama = URLEncoder.encode(etNama.getText().toString(), "utf-8");
url += "?code=" + kode ;
getRequest(url);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void getRequest(String Url) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
// The default value is zero, that means the timeout is not used.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
httpClient.setParams(httpParameters);
if(timeoutConnection > 3000){
Toast.makeText(this, "Request Timeout", Toast.LENGTH_LONG).show();
System.out.println("Request Timeout");
}
HttpResponse response = client.execute(request);
String res = EntityUtils.toString(response.getEntity());
//Toast.makeText(this, "Tambah Data " + request(response) ,Toast.LENGTH_SHORT).show();
System.out.println(res);
etNama.setText(res);
}
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;
}