最新更新
package com.karanvir.again;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
EditText city;
Button go;
TextView resultofw;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
city=(EditText) findViewById(R.id.editText);
go=(Button) findViewById(R.id.button);
setSupportActionBar(toolbar);
DownloadTask task=new DownloadTask();
resultofw=(TextView) findViewById(R.id.textView);
task.execute("http://api.apixu.com/v1/current.json?key=76d62bf509e64633a4970055170706&q="+city.toString());
}
public class DownloadTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//conver result into json object
try{
JSONObject jsonObject=new JSONObject(result);
//get current JSONObject from result JSONObject
JSONObject currentJSONObject = jsonObject.getJSONObject("current");
//Now get condition JSONObject from current JSONObject
JSONObject conditionJSONObject = currentJSONObject.getJSONObject("condition");
text = conditionJSONObject.getString("text");
//Now print condition JSONObject
Log.i("website content", "condition json object : " + text.toString());
JSONArray arr= new JSONArray(conditionJSONObject.toString());
} catch (Exception e){
}
}
}
public void click(View view){
resultofw.setText(text);
}
}
&#13;
那我怎么能解决这个问题呢,我试着把字符串添加到数组中。然后在&#34; text&#34;
之后拉出所说的内容
//conver result into json object
try{
JSONObject jsonObject=new JSONObject(result);
//get current JSONObject from result JSONObject
JSONObject currentJSONObject = jsonObject.getJSONObject("current");
//Now get condition JSONObject from current JSONObject
JSONObject conditionJSONObject = currentJSONObject.getJSONObject("condition");
//Now print condition JSONObject
Log.i("website content", "condition json object : " + conditionJSONObject.toString());
JSONArray arr= new JSONArray(conditionJSONObject.toString());
for(int i=0;i<arr.length();i++){
JSONObject jsonpart= arr.getJSONObject(i);
Log.i("Part",jsonpart.getString("text"));
}
&#13;
我想要做的是在&#34;&#34;条件&#34;: {&#34;文字&#34;:&#34;部分多云&#34后打印所有内容;,&#34;图标&#34;:&#34; // cdn.apixu.com/weather/64x64/day/116.png","code":1003}, 34; wind_mph&#34; 15.0&#34; wind_kph&#34;:24.1,&#34; wind_degree&#34;:240,&#34; wind_dir&#34;:&#34; WSW&#34;,& #34; pressure_mb&#34;:1016.0,&#34; pressure_in&#34;:30.5,&#34; precip_mm&#34;:0.0&#34; precip_in&#34;:0.0&#34;湿度&#34 ;:71,&#34;云&#34; 50&#34; feelslike_c&#34;:9.6,&#34; feelslike_f&#34;:49.2,&#34; vis_km&#34;:10.0,&# 34; vis_miles&#34;:6.0}}&#34; 到我的日志,我 使用JSONG处理来自此网站的数据:http://api.apixu.com/v1/current.json?key=76d62bf509e64633a4970055170706&q=Paris 它没有出现在我的logcat中,请帮助 并且我已包含在我的MENIFEST中使用许可
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.karanvir.again.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
&#13;
06-07 01:42:19.594 20523-20523/com.karanvir.again E/Zygote: v2
06-07 01:42:19.594 20523-20523/com.karanvir.again I/libpersona: KNOX_SDCARD checking this for 10291
06-07 01:42:19.594 20523-20523/com.karanvir.again I/libpersona: KNOX_SDCARD not a persona
06-07 01:42:19.594 20523-20523/com.karanvir.again I/SELinux: Function: selinux_compare_spd_ram, index[1], SPD-policy is existed. and_ver=SEPF_SM-G870W_5.0 ver=38
06-07 01:42:19.594 20523-20523/com.karanvir.again W/SELinux: Function: selinux_compare_spd_ram, index[1], priority [2], priority version is VE=SEPF_SECMOBILE_6.0.1_0013
06-07 01:42:19.594 20523-20523/com.karanvir.again E/Zygote: accessInfo : 0
06-07 01:42:19.594 20523-20523/com.karanvir.again W/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=com.karanvir.again
06-07 01:42:19.594 20523-20523/com.karanvir.again I/art: Late-enabling -Xcheck:jni
06-07 01:42:19.974 20523-20523/com.karanvir.again W/ResourcesManager: getTopLevelResources: /data/app/com.karanvir.again-2/base.apk / 1.0 running in com.karanvir.again rsrc of package null
06-07 01:42:20.064 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.304 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.364 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.414 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.484 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.544 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.614 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.694 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.754 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.814 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.894 20523-20523/com.karanvir.again W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=krait --instruction-set-features=default --dex-file=/data/app/com.karanvir.again-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.karanvir.again-2@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
06-07 01:42:20.894 20523-20523/com.karanvir.again W/System: ClassLoader referenced unknown path: /data/app/com.karanvir.again-2/lib/arm
06-07 01:42:20.914 20523-20523/com.karanvir.again I/InstantRun: starting instant run server: is main process
06-07 01:42:20.994 20523-20523/com.karanvir.again W/ResourcesManager: getTopLevelResources: /data/app/com.karanvir.again-2/base.apk / 1.0 running in com.karanvir.again rsrc of package null
06-07 01:42:20.994 20523-20523/com.karanvir.again W/ResourcesManager: getTopLevelResources: /data/app/com.karanvir.again-2/base.apk / 1.0 running in com.karanvir.again rsrc of package null
06-07 01:42:21.114 20523-20523/com.karanvir.again W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-07 01:42:21.424 20523-20780/com.karanvir.again I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
06-07 01:42:21.424 20523-20780/com.karanvir.again I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
06-07 01:42:21.464 20523-20787/com.karanvir.again I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: (Ia10634f51b)
OpenGL ES Shader Compiler Version: E031.29.00.00
Build Date: 01/28/16 Thu
Local Branch: ss
Remote Branch:
Local Patches:
Reconstruct Branch:
06-07 01:42:21.474 20523-20787/com.karanvir.again I/OpenGLRenderer: Initialized EGL, version 1.4
06-07 01:42:21.544 20523-20523/com.karanvir.again W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
06-07 01:42:21.574 20523-20523/com.karanvir.again I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@5600697 time:300881669
&#13;
package com.karanvir.again;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DownloadTask task=new DownloadTask();
task.execute("http://api.apixu.com/v1/current.json?key=76d62bf509e64633a4970055170706&q=Paris");
}
public class DownloadTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//conver result into json object
try{
JSONObject jsonObject=new JSONObject(result);
String xx=jsonObject.getString("condition");
Log.i("website content", xx);
} catch (Exception e){
}
}
}
}
&#13;
答案 0 :(得分:1)
实际上,condition
是JSONObject
所以你需要从结果JSONObject解析它,如下所示。
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//conver result into json object
try{
JSONObject jsonObject=new JSONObject(result);
//get current JSONObject from result JSONObject
JSONObject currentJSONObject = jsonObject.getJSONObject("current");
//Now get condition JSONObject from current JSONObject
JSONObject conditionJSONObject = currentJSONObject.getJSONObject("condition");
//Now print condition JSONObject
Log.i("website content", "condition json object : " + conditionJSONObject.toString());
} catch (Exception e){
}
}
<强>更新强>
只需在getString
上调用conditionJSONObject
方法即可获得&#34; text&#34;的值如下所示
String text = conditionJSONObject.getString("text");
答案 1 :(得分:0)
Log.d(&#34;网站内容&#34;,jsonObject.getString(&#34;条件&#34;)。toString());