我做了一个按钮,当我单击数据时,应该在该按钮上显示。但事实并非如此。我不知道出什么问题了。我没有任何错误,该应用程序也可以正常运行。
我已经授予了Internet许可,所以没有问题。
这是代码文件
MainActivity.java
package com.example.apiactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button click;
public static TextView data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click=(Button) findViewById(R.id.button);
data=(TextView) findViewById(R.id.fetcheddata);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchData process= new fetchData();
process.execute();
}
});
}
}
fetchData.java
package com.example.apiactivity;
import android.os.AsyncTask;
import org.json.JSONArray;
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.MalformedURLException;
import java.net.URL;
public class fetchData extends AsyncTask<Void,Void,Void> {
String data="";
String dataParsed="";
String singleParsed="";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url=new URL("https://oneglobal.in/api/UserApi/Login");
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream));
String line="";
while (line!=null){
line=bufferedReader.readLine();
data= data + line;
}
JSONArray JA=new JSONArray(data);
for (int i=0;i<JA.length();i++){
JSONObject JO= (JSONObject) JA.get(i);
singleParsed= "Email:" + JO.get("Email") + "\n" + "Password:" + JO.get("Password") + "\n";
dataParsed = dataParsed+ singleParsed + "\n";
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.data.setText(this.data);
}
}
activity_main.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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLick me!"
android:id="@+id/button"
android:layout_marginBottom="10dp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:textSize="24sp"
android:id="@+id/fetcheddata"
android:hint="Fetched data here"
/>
</ScrollView>
</RelativeLayout>
每当我单击按钮时,我都希望在表格视图中的屏幕上显示json数据。
答案 0 :(得分:0)
您必须将用户名和密码发送到登录api 最好使用翻新甚至凌空与远程api集成