Last active
November 3, 2015 15:16
-
-
Save peter279k/8fa9a9a2102743863708 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.request_web; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.telephony.TelephonyManager; | |
import android.view.Menu; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.Toast; | |
public class RequestActivity extends Activity { | |
Button visit_btn; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setTitle("拜訪網站"); | |
setContentView(R.layout.activity_request); | |
visit_btn = (Button) findViewById(R.id.button1); | |
visit_btn.setOnClickListener(listener1); | |
} | |
OnClickListener listener1 = new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); | |
String userName = telManager.getDeviceId(); | |
String userPass = telManager.getSubscriberId(); | |
visitWeb(userName, userPass); | |
} | |
}; | |
private void visitWeb(String userName, String userPass) { | |
class visitWebAsyncTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... arg0) { | |
// TODO Auto-generated method stub | |
String userName = arg0[0]; | |
String userPass = arg0[1]; | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpPost httpPost = new HttpPost("http://peter279k.com/visit_web/visit.php"); | |
BasicNameValuePair userNameBasicNameValuePair = new BasicNameValuePair("user_name", userName); | |
BasicNameValuePair passwordBasicNameValuePair = new BasicNameValuePair("password", userPass); | |
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); | |
nameValuePairList.add(userNameBasicNameValuePair); | |
nameValuePairList.add(passwordBasicNameValuePair); | |
try { | |
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList); | |
httpPost.setEntity(urlEncodedFormEntity); | |
try { | |
HttpResponse httpResponse = httpClient.execute(httpPost); | |
InputStream inputStream = httpResponse.getEntity().getContent(); | |
InputStreamReader inputStreamReader = new InputStreamReader(inputStream); | |
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); | |
StringBuilder stringBuilder = new StringBuilder(); | |
String bufferedChunkStr = null; | |
while((bufferedChunkStr = bufferedReader.readLine()) != null) { | |
stringBuilder.append(bufferedChunkStr); | |
} | |
return stringBuilder.toString(); | |
} | |
catch(ClientProtocolException cpe) { | |
cpe.printStackTrace(); | |
} | |
catch(IOException ioe) { | |
ioe.printStackTrace(); | |
} | |
} | |
catch(UnsupportedEncodingException uee) { | |
uee.printStackTrace(); | |
} | |
return null; | |
} | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
if(result.equals("invalid")) { | |
Toast.makeText(getApplicationContext(), "Http post is invalid...", Toast.LENGTH_LONG).show(); | |
} | |
else if(result.contains("visits this web.")){ | |
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show(); | |
} | |
else { | |
Toast.makeText(getApplicationContext(), "unknown http post response.", Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
visitWebAsyncTask visitWebAsync = new visitWebAsyncTask(); | |
visitWebAsync.execute(userName, userPass); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.request, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment