다음은 JSON 객체를 가져 오는 클래스입니다. 이 코드에서는 하나의 객체 만 얻었고 메서드에서 반환하는 방법을 모르지만 Protected Void 메서드가 있습니다.이 메서드는 settext 메서드이며 유일한 JSON 개체가있는 곳입니다. I 할 원하는Json MySql은 두 개의 정수와 문자열을 얻습니다.
public class ConnectMySql extends Activity {
TextView httpStuff;
HttpClient client;
JSONObject json;
final static String URL = "http://79.114.48.119/RadarsMySql.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
httpStuff = (TextView) findViewById(R.id.tvHttp);
client = new DefaultHttpClient();
new Read().execute("latitude");
}
public JSONObject lastTweet(String username) throws ClientProtocolException, IOException,JSONException{
StringBuilder url = new StringBuilder(URL);
url.append(username);
HttpGet get = new HttpGet(url.toString());
HttpResponse r = client.execute(get);
int status = r.getStatusLine().getStatusCode();
//if(status == 200){
HttpEntity e = r.getEntity();
String data = EntityUtils.toString(e);
data = data.substring(data.indexOf("["));
JSONArray timeline = new JSONArray(data);
JSONObject last = timeline.getJSONObject(0);
return last;
//}else{
//Toast.makeText(ConnectMySql.this, "error", Toast.LENGTH_LONG);
//return null;
//}
}
public class Read extends AsyncTask<String, Integer, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
json = lastTweet("");
return json.getString(params[0]);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
public void onPostExecute(String result) {
// TODO Auto-generated method stub
httpStuff.setText(result);
int myNum = 0;
try {
myNum = Integer.parseInt(result);
httpStuff.setText(myNum);
} catch(NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}
}
}
}
내가 개체 세 종류 (exemple 위도를 저장할 수있는 배열을 갖는 [1] 경도 [1]에 설명 [1] 찾기 [2] 등등 위도와 경도를 정수로 사용하고 싶습니다.) 이후에 for 루프를 사용하여이 3 개의 매개 변수로 함수를 호출합니다. 아무도 나를 도울 수 없거나 나에게 몇 가지 조언을 줄 수 있습니까? 감사합니다.
아무도 도와 줄 수 없나요? –
네 친구가 말해줘. – Akram
아무도 모릅니다? ? –