그물 전체의 코드 예제를 사용하여 내 서버에서 정확하게 .php 파일을 호출하고 JSON 데이터를 검색 한 다음 데이터를 구문 분석하고, 그것을 인쇄하십시오.원격 데이터베이스를 통해 가져온 데이터를 조작 한 후 데이터를 조작하는 방법
문제는 내가 따르고있는 자습서를 위해 화면에 인쇄하는 것이지만 지금은 다른 곳에서 해당 데이터를 사용해야하고 그 프로세스를 파악하는 데 도움이 필요하다는 것입니다.
궁극적 인 목표는지도 좌표로 내 db 쿼리를 반환 한 다음 Google지도에 플롯하는 것입니다. 지도에 수동으로 점을 표시하는 또 다른 앱이 있으므로 반환 된 데이터를 올바르게 조작하는 방법에 대해 머리를 맞춰야 만이 앱을 통합 할 수 있습니다.
public class Remote extends Activity {
/** Called when the activity is first created. */
TextView txt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create a crude view - this should really be set via the layout resources
// but since its an example saves declaring them in the XML.
LinearLayout rootLayout = new LinearLayout(getApplicationContext());
txt = new TextView(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
// Set the text and call the connect function.
txt.setText("Connecting...");
//call the method to run the data retreival
txt.setText(getServerData(KEY_121));
}
public static final String KEY_121 = "http://example.com/mydbcall.php";
private String getServerData(String returnString) {
InputStream is = null;
String result = "";
//the year data to send
//ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("year","1970"));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","longitude: "+json_data.getDouble("longitude")+
", latitude: "+json_data.getDouble("latitude")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
그래서 코드 :
returnString += "\n\t" + jArray.getJSONObject(i);
현재 화면에 인쇄하는 것입니다.
은 내가 파악해야하는 것은 내가 프로그램에서 다른 지점에서 참조 할 수있는 무언가로 데이터를 얻고, 각각의 요소에 액세스 즉하는 방법입니다 :
double longitude = jArray.getJSONObject(3).longitude;
또는 뭔가를 그 효과에 ..
클래스 getServerData가 Array 유형 또는 무엇을 반환해야하는지 알 수 있습니까?
감사합니다. 감사합니다.
내가 설명한대로, 내 수업의 배열 인 결과를 반환합니다. 위치. 간단한 질문, result.get (location);과 같이 반환 된 데이터에 한 번 액세스하는 방법은 무엇입니까? – bMon
Java의 배열과 목록을 읽어야합니다. 그것들은 꽤 기본적인 것들이며 많은 문서들입니다 : http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html http://download.oracle.com/javase/tutorial/collections /interfaces/list.html –
감사합니다. Mayra, 고맙습니다. – bMon