[我知道我在stackoverflow上看到了相同的问题,并尝试了很多解决方法,但是我失败了,我是android studio的新手,只想读取json文件,但它总是抛出此错误,请不要给我负面声誉或提出重复的问题。我是初学者,这是我第五天在这里度过功能非常简单,它将从服务器读取json文件并在mainactivity中打印它,一个活动将是fetch活动将在后台执行工作
这是我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<Button
android:id="@+id/parse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="683dp"
android:text="parse" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/parse">
<TextView
android:id="@+id/prasedata"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="fetch text here" />
</ScrollView>
</RelativeLayout>
这是主要活动
package com.codewithosama.volleydemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.jar.JarEntry;
public class MainActivity extends AppCompatActivity {
Button click ;
public static TextView data ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
click = (Button) findViewById(R.id.parse);
data = (TextView)findViewById(R.id.prasedata);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchData process =new fetchData();
process.execute();
}
});
}
}
这是fetchDAta活动
package com.codewithosama.volleydemo;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class fetchData extends AsyncTask<Void, Void, String> {
String data="";
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL("https://jsonplaceholder.typicode.com/todos/1");
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
InputStream inputStream = httpsURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line="";
while (line != null){
line=bufferedReader.readLine();
data= data + line;
}
}catch (MalformedURLException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
MainActivity.data.setText(this.data);
}
}
您忘记了在setContentView()
中呼叫MainActivity
。所以应该是
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main) // your activity's layout
// rest of your code
}