|
|
|
สอบถาม Android Json แล้วจะตัดเอาเฉาะข้อความที่ใช้ได้อย่าไรครับ |
|
|
|
|
|
|
|
ผมทำ android post ค่าไปยัง php แล้วแสดงข้อความที่ php ดึงจากฐานข้อมูลแต่มันเป็น json จะตัดเอาเฉพาะข้อความยังไงครับ
Code (PHP)
mysql_connect($host,$user,$pass);
mysql_query("SET NAMES UTF8");
mysql_query("USE $db");
$compound = $_REQUEST['compound'];
$sql = "SELECT effect FROM effect_db WHERE codename='$compound'";
$result = mysql_query($sql);
if(isset($_POST["compound"])){
if($result){
while($row = mysql_fetch_array($result))
$data[] = $row;
$json = array('status' => "OK",'result' => $data);
}else{
$json = array('status' => "ERROR");
}
}else{
$json["result"]="error";
}
print(json_encode($json));
mysql_close();
?>
Code (Android-Java)
package example.testjson;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView result;
private Context context;
private MyHTTP_Post poster;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
result = (TextView) findViewById(R.id.result);
String compound_id = "D0001";
poster = new MyHTTP_Post("http://192.168.1.8/myweb/jsondrugeffect.php");
ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("compound", compound_id));
poster.doPost(data, new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case MyHTTP_Post.HTTP_POST_ERROR:
result.setText((String) msg.obj);
break;
case MyHTTP_Post.HTTP_POST_OK:
String jsonString = (String) msg.obj;
try {
JSONObject object = new JSONObject(jsonString);
String name_effect = object.getString("result");
MainActivity.this.result.setText(name_effect);
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "JSON ERROR!!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
break;
}
};
});
}
}
httppost .java (Android-Java)
package example.testjson;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.os.Handler;
import android.os.Message;
public class MyHTTP_Post {
private HttpClient client;
private HttpPost post;
private HttpResponse response;
private Thread thread;
public static final int HTTP_POST_OK = 1;
public static final int HTTP_POST_ERROR = 2;
public MyHTTP_Post(String link) {
client = new DefaultHttpClient();
post = new HttpPost(link);
}
public void doPost(final ArrayList<NameValuePair> data,
final Handler handler) {
thread = new Thread() {
@Override
public void run() {
Message message = new Message();
try {
post.setEntity(new UrlEncodedFormEntity(data, HTTP.UTF_8));
response = client.execute(post);
message.what = HTTP_POST_OK;
message.obj = EntityUtils.toString(response.getEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
message.what = HTTP_POST_ERROR;
} catch (ClientProtocolException e) {
e.printStackTrace();
message.what = HTTP_POST_ERROR;
} catch (Exception e) {
e.printStackTrace();
message.what = HTTP_POST_ERROR;
} finally {
handler.sendMessage(message);
}
}
};
thread.start();
}
}
Tag : Mobile, Android, Mobile
|
ประวัติการแก้ไข 2013-07-27 18:11:45
|
|
|
|
|
Date :
2013-07-27 18:09:06 |
By :
tarhunter |
View :
1481 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android and JSON
|
|
|
|
|
Date :
2013-07-27 19:32:28 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|