안 드 로 이 드 자체 테이프 의 JSONobject 를 이용 하여 json 데 이 터 를 분석 합 니 다.

4363 단어 Android
할 말 이 많 지 않 으 니 코드 를 직접 입력 하 세 요.
 
json 데이터:http://www.haoservice.com/docs/6
레이아웃:
 


    
 
主文件:
		
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import static android.content.ContentValues.TAG; public class MainActivity extends Activity { //      private Button button; private final String address = "http://apis.haoservice.com/weather?cityname=  &key=  Key"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //          button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //              new Thread(new Runnable() { @Override public void run() { try { URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); InputStream in = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder builder = new StringBuilder(); String line; //         while ((line = reader.readLine()) != null) { builder.append(line); //            String data = builder.toString(); //      ,       json   parseison(data); //       reader.close(); in.close(); conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } private void parseison(String data) { try { //           JSONObject jsonobject = new JSONObject(data); int error_code = jsonobject.getInt("error_code"); if (error_code == 0) { JSONObject jsonobject_1 = jsonobject.getJSONObject("result"); //       JSONObject json_2 = jsonobject_1.getJSONObject("sk"); String temp = json_2.getString("temp"); String wind_direction = json_2.getString("wind_direction"); String wind_strength = json_2.getString("wind_strength"); String humidity = json_2.getString("humidity"); String time = json_2.getString("time"); Log.d(TAG, "    :"+temp+"
"+" :"+wind_direction+"
"+" :"+wind_strength+"
"+" :"+humidity+"
"+" :"+time); // JSON JSONObject today = jsonobject_1.getJSONObject("today"); String output = today.getString("city")+"
"+ today.getString("date_y")+"
"+ today.getString("week")+"
"+ today.getString("temperature")+"
"+ today.getString("weather")+"
"+ today.getString("fa")+"
"+ today.getString("fb")+"
"+ today.getString("wind")+"
"+ today.getString("dressing_index")+"
"+ today.getString("dressing_advice")+"
"+ today.getString("uv_index")+"
"+ today.getString("comfort_index")+"
"+ today.getString("wash_index")+"
"+ today.getString("travel_index")+"
"+ today.getString("exercise_index")+"
"+ today.getString("drying_index"); Log.d(TAG, " :"+output); } else { Log.d(TAG, "parseison: "); } } catch (JSONException e) { e.printStackTrace(); } } }); } }

 
 

 
 
 

 
 

좋은 웹페이지 즐겨찾기