public class POSTActivity extends AppCompatActivity implements View.OnClickListener {
TextView tvIsConnected;
Button btnPost;
String TAG = "json";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activity_second);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btnPost.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.btnPost:
if (!validate())
new HttpAsyncTask().execute("http://zhaksy-adam.kz/api/Requisitions/PostRequisition");
break;
}
}
private class HttpAsyncTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
public static String POST(String url){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.add("CityID", "1");
jsonObject.add("TypeID","1");
jsonObject.add("Title", "SomeTitle");
jsonObject.add("RegionID", "1");
jsonObject.add("Phone1", "+7(705)105-78-70");
jsonObject.add("Decription","<p>Some Description</p>");
jsonObject.add("Date", "29-02-2016");
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("accept", "json");
httpPost.setHeader("Content-type", "json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
{ result = convertInputStreamToString(inputStream);
Log.d("json","inputStream result"+result);}
else
result = "Did not work!";
Log.d("json","result"+result);
} catch (Exception e) {
Log.d("json","e.getLocalizedMessage()"+ e.getLocalizedMessage());
}
// return result
return result;
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
Trang Chủ
Android
PHP
Chuỗi
Dialog
Clipboard
Spinner
Auto Layout
ActionBar
Intent
Random
SQL
Admod
Version
TextView
Tabhost
Json
ListView
Time Picker
Calendar
Ham
FloatingActionMenu
Button
EditText
Activity
Custom Adapter
Xoay Màn Hình
Wifi & 3G
Lỗi
Fragment
Shape
SMS
Push
FireBase
CheckBox
Mảng
Facebook
Timer
Service
Text To Speech
Recycler View
Search This Blog
Thursday, October 27, 2016
Service - Gửi dữ liệu Post lên api
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment