Json_insert
//Json insert
//build.gradle(mdule:app) ma [compileSdkVersion 26] ni niche vadi library lkhvani
//useLibrary 'org.apache.http.legacy' aa library lkhvani
//manifest ma permission leve
public class MainActivity extends AppCompatActivity {
EditText name,pass;
Button insert;
String json="";
InputStream is=null;//InputStream is used to write the data to a file or something
JSONObject jobj=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//StrictMode is a developer tool which detects things you might be doing
// by accident and brings them to your attention so you can fix them.
//StrictMode is most commonly used to catch accidental disk or network
// access on the application's main thread, where UI operations are received and animations take place.
name=findViewById(R.id.etnamae);
pass=findViewById(R.id.etpass);
insert=findViewById(R.id.btninsert);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InsertTask it=new InsertTask();//insertTask inner class take
it.execute();
}
});
}
private class InsertTask extends AsyncTask{
String url="http://192.168.43.34/json/insert.php";//here json means folder name and insert ie folder ni umder je php file hoy aenu name
@Override
protected Object doInBackground(Object[] objects) {
ArrayList<NameValuePair> list=new ArrayList<>();
list.add(new BasicNameValuePair("name",name.getText().toString()));
list.add(new BasicNameValuePair("pass",pass.getText().toString()));
DefaultHttpClient client=new DefaultHttpClient();// it will create a client with whom we need to connect with the internet servies.
HttpPost post=new HttpPost(url);//It will use the php files for connection with the server on xampp.
try {
post.setEntity(new UrlEncodedFormEntity(list));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try{
HttpResponse response=client.execute(post);//server is reponding.
HttpEntity entity=response.getEntity();
is=entity.getContent();// whatever the data entity get it will write through inputstream.
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jobj;
}
}
}
Comments
Post a Comment