so i'm new to android development and i just learned about ASyncTask. I'm using it in one of my app and still i am getting the warning that: "The application may be doing too much work on its main thread" and it is also skipping frames.
I'm parsing a json text from a my website which pulls data from a database and converts it into json.
TextView t1;
TextView t2;
TextView t3;
EditText editText;
public boolean checkNumber(String n){
boolean flag=true;
for(int i=0;i<n.length();i++){
char ch=n.charAt(i);
if(!(Character.isDigit(ch))){
flag=false;
break;
}
}
return flag;
}
public static class getContent extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
String r="";
URL url;
HttpURLConnection httpURLConnection;
try {
url=new URL(urls[0]);
httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream in=httpURLConnection.getInputStream();
InputStreamReader reader=new InputStreamReader(in);
int data=reader.read();
while(data != -1){
char ch=(char) data;
r=r+ch;
data=reader.read();
}
return r;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
public void findUser(View view) throws ExecutionException, InterruptedException, JSONException {
t1=findViewById(R.id.nameView);
t2=findViewById(R.id.emailView);
t3=findViewById(R.id.countryView);
editText=findViewById(R.id.numberEditText);
String s=editText.getText().toString();
if((s.length()==0) || (!checkNumber(s))){
Toast.makeText(this, "Please enter a number...", Toast.LENGTH_SHORT).show();
}else{
getContent getContent=new getContent();
String json=getContent.execute("http://www.website.com/Test/index.php?id="+s).get();
JSONObject jsonObject=new JSONObject(json);
String error=jsonObject.getString("error");
if(error.equals("false")){
String name=jsonObject.getString("name");
String email=jsonObject.getString("email");
String country=jsonObject.getString("country");
t1.setText("Name: "+name);
t2.setText("Email: "+email);
t3.setText("Country: "+country);
}else{
Toast.makeText(this, "Cannot Find a user...", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
<?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="net.vishurules.jsondata.MainActivity">
<EditText
android:id="@+id/numberEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="58dp"
android:ems="10"
android:hint="@string/pick_a_number"
android:inputType="number" />
<Button
android:id="@+id/goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/numberEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:onClick="findUser"
android:text="@string/go" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/goButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="125dp"
android:text="@string/name"
android:textSize="32sp" />
<TextView
android:id="@+id/emailView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/nameView"
android:layout_below="@+id/nameView"
android:layout_marginTop="15dp"
android:text="@string/email"
android:textSize="32sp" />
<TextView
android:id="@+id/countryView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/emailView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="@string/country"
android:textSize="32sp" />
</RelativeLayout>