안 드 로 이 드 자체 테이프 의 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(); } } }); } }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.