|
|
|
Android ทำ popup เด้งเวลากรอก ip และ port ผิด พอทำแล้ว popup เด้งแต่ไม่สามารถลิงค์ไปอีกหน้าหนึ่งได้ |
|
|
|
|
|
|
|
พอดีจะทำ popup แจ้งเตือนกรณ๊ที่กรอก IP และ Port จากค่าที่รับมาผิด แต่พอกรอกแล้ว popup เด้ง แต่ไม่สามารถลิงค์ไปยังอีกหน้าหนึ่งได้ค่ะ พอจะมีวิธีแก้ไหมค่ะ
โค้ดในส่วนของจาวา
Code (Java)
package com.example.project1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.R.integer;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText getip ;
EditText getport;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getip = (EditText)findViewById(R.id.editText1);
getport = (EditText)findViewById(R.id.EditText01);
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
Button btnStart = (Button)findViewById(R.id.button1);
Button btnClose = (Button)findViewById(R.id.Button2);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String MyGetIp = getip.getText().toString();
String RespondConnect = GetHostData(MyGetIp);//GET(MyGetIp);
Log.e("A",MyGetIp);
Log.e("IP",RespondConnect);
adb.setTitle("IP หรือ Prot ผิดพลาด");
adb.setMessage("กรุณากรอก IP หรือ Port ใหม่ค่ะ");
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Your Click OK. " ,
Toast.LENGTH_LONG).show();
}
});
adb.show();
}
});
btnClose.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
//String MyGetIp = "";
String RespondConnect = "";
public void send(View v)
{
String MyGetIp = getip.getText().toString();
//String RespondConnect = ""; //= GetHostData(MyGetIp);//GET(MyGetIp);
/*
new Thread(new Runnable() {
public void run() {
RespondConnect = GetHostData(MyGetIp);//GET(MyGetIp)
}
}).start();
*/
//new HttpAsyncTask().execute("http://hmkcode.com/examples/index.php");
String ExeIP = "http://" + MyGetIp;
new HttpAsyncTask().execute(ExeIP);
//Log.e("A",MyGetIp);
Log.e("Rx",RespondConnect);
String[] RxPackage = RespondConnect.split("\\|");
int LentPack = RxPackage.length;
Log.e("Length", String.valueOf(LentPack));
if(LentPack>10)
{
String GetOK = RxPackage[1].trim();
Log.e("Pack",GetOK);
//////// to control ////////
if(GetOK.equals("OK"))
{
Intent itn = new Intent(this,Control.class);
itn.putExtra("IP", MyGetIp);
startActivity(itn);
}
////////////////////////////
}
//Log.e("IP",RespondConnect);
}
public String GetHostData(String url)
{
BufferedReader in = null;
String data = null;
String line = "";
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
//URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
URI website = new URI(url);
//URI website = new URI("http://mthai.com");
request.setURI(website);
HttpResponse response = httpclient.execute(request);
in = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
// NEW CODE
line = in.readLine();
//textv.append(" First line: " + line);
// END OF NEW CODE
//textv.append(" Connected ");
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
return(line);
}
public String GET(String url){
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private 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;
}
// check network connection
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return GET(urls[0]);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
getport.setText(result);
RespondConnect = result;
//Log.e("port",result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Tag : Mobile, Android, JAVA
|
|
|
|
|
|
Date :
2015-06-02 13:21:44 |
By :
wanniga |
View :
1240 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|