I'm very new to android programming. I have a program that uses sockets to connect to a remote host, spoof an http request, and parse the results. For some reason, my socket cannot connect to the host. I have tested the program in a standalone environment and it connects and works fine. I went ahead and isolated the function I needed and included it in my activity class to call it that way. Ive also tried calling it staticly, as well as executing` an asynchronous object and calling it from there. The internet on my android emulator works from the browser, and i've added the
To my android manifest. The code below shows where I stand right now. What do do I need to do?
public class FlightNumResults extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String value = intent.getStringExtra("input"); //if it's a string you stored.
//super.onCreate(savedInstanceState);
System.out.println("HULLO");
System.out.println(value);
setContentView(R.layout.loading);
Log.i("Excecution", "Complete");
Hashtabletable = flightNumInfo(value);
if (table == null)
System.out.println("NULL");
TableLayout results = (TableLayout) findViewById(R.id.resultTable);
TableRow rowLabels = new TableRow(this);
TableRow rowResults = new TableRow(this);
rowLabels.setGravity(Gravity.CENTER);
TableRow.LayoutParams params = new TableRow.LayoutParams();
params.span = 4;
//Column 1
TextView destLabel = new TextView(this);
destLabel.setText("Destination");
destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView destResult = new TextView(this);
destResult.setText(table.get("destination"));
destResult.setGravity(Gravity.CENTER_HORIZONTAL);
rowLabels.addView(destLabel);
rowResults.addView(destResult);
//Column 2
TextView gateLabel = new TextView(this);
destLabel.setText("Gate");
destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView gateResult = new TextView(this);
destResult.setText(table.get("gate"));
destResult.setGravity(Gravity.CENTER_HORIZONTAL);
rowLabels.addView(gateLabel);
rowResults.addView(gateResult);
//Column 3
TextView scheduledLabel = new TextView(this);
destLabel.setText("Scheduled");
destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView scheduledResult = new TextView(this);
destResult.setText(table.get("scheduled"));
destResult.setGravity(Gravity.CENTER_HORIZONTAL);
rowLabels.addView(scheduledLabel);
rowResults.addView(scheduledResult);
//Column 4
TextView estimatedLabel = new TextView(this);
destLabel.setText("estimated");
destLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
TextView estimatedResult = new TextView(this);
destResult.setText(table.get("scheduled"));
destResult.setGravity(Gravity.CENTER_HORIZONTAL);
rowLabels.addView(estimatedLabel);
rowResults.addView(estimatedResult);
results.addView(rowLabels);
results.addView(rowResults);
setContentView(R.layout.activity_flightnum_results);
}
public HashtableflightNumInfo(String flightNum) {
Hashtabletable = new Hashtable ();
System.out.println("Initializing socket");
try {
String request = "GET /flifo/servlet/DeltaFlifo?airline_code=DL&flight_number="
+ flightNum
+ "&flight_date="
+ DateCalc.getSlashDateAndTime()
+ "&request=main&DptText=ATL HTTP/1.1";
Socket conn = new Socket(InetAddress.getByName("www.delta.com"), 80);
System.out.println("initialized");
PrintWriter wr = new PrintWriter(conn.getOutputStream());
wr.println(request);
wr.println("Host: www.delta.com");
wr.println("Referer: http://www.delta.com/flifo/servlet/DeltaFlifo?airline_code=DL&request=main");
wr.println("Connection: close\n");
wr.flush();
Scanner in = new Scanner(conn.getInputStream());
String myResp = new String();
String ans = new String();
do {
ans = in.nextLine();
myResp += ans;
} while (ans.indexOf("
") < 0);
wr.close();
in.close();
// System.out.println(myResp);
Pattern p = Pattern
.compile("\\s+Atlanta\\s+" \\s+
+ ""
+ "\\(ATL\\)\\s+
(.*?)"
+ "
Gate ([A-Z0-9]{2,3})\\s+"
+ "\\s+ ([A-Za-z0-9:]{6,9}) \\s+"
\\s+ (.*?)
+ "\\s+ ([a-z0-9*?:]{6,9}|[a-zA-Z\\s]{6,9}) \\s+"
\\s+ (.*?)
+ "(.*?)\\(([A-Z]{3})\\)");
Matcher m = p.matcher(myResp);
if (m.find()) {
table.put("number", flightNum);
table.put("gate", m.group(2));
table.put("scheduled", m.group(3));
table.put("estimated", m.group(5));
table.put("destination", m.group(8));
return table;
}
} catch (Exception e) {
return null;
}
return null;
}
}Here is what my code looks like using AsyncTask.
public class FlightNumResults extends Activity {
public String flightNum;
public Hashtabletable;
@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
flightNum = intent.getStringExtra("input"); // if it's a string you
// stored.
super.onCreate(savedInstanceState);
System.out.println("HULLO");
// System.out.println(value);
setContentView(R.layout.loading);
Log.i("Excecution", "Complete");
FlightInfo fi = new FlightInfo();
fi.execute();
fi.flightNumInfo(flightNum);
// Hashtabletable = fi.flightNumInfo(value);
// System.out.println(table.get("destination"));
// setContentView(R.layout.activity_flightnum_results);
}
public class FlightInfo extends AsyncTask{
public Socket conn;
public PrintWriter wr;
public DataInputStream dis;
public DataOutputStream dos;
@Override
protected Void doInBackground(Void... params) {
try {
conn = new Socket(InetAddress.getByName("www.delta.com"), 80);
}catch (Exception e){
Log.i("AsyncTank", "Can't create socket");
}
if (conn.isConnected()){
try {
dis = (DataInputStream)conn.getInputStream();
dos = (DataOutputStream)conn.getOutputStream();
Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
e.printStackTrace();
}
} else {
Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket is closed");
}
return null;
}
protected void onPostExecute(Boolean result) {
if (result) {
Log.i("AsyncTask", "onPostExecute: Completed with an Error.");
} else {
Log.i("AsyncTask", "onPostExecute: Completed.");
}
setContentView(R.layout.activity_flightnum_results);
}
public void flightNumInfo(String flightNum) {
table = new Hashtable();
String request = "GET /flifo/servlet/DeltaFlifo?airline_code=DL&flight_number="
+ flightNum
+ "&flight_date="
+ DateCalc.getSlashDateAndTime()
+ "&request=main&DptText=ATL HTTP/1.1";
//Socket conn = new Socket(InetAddress.getByName("www.delta.com"), 80);
wr = new PrintWriter(dos);
wr.println(request);
wr.println("Host: www.delta.com");
wr.println("Referer: http://www.delta.com/flifo/servlet/DeltaFlifo?airline_code=DL&request=main");
wr.println("Connection: close\n");
wr.flush();
Scanner in = new Scanner(dis);
String myResp = new String();
String ans = new String();
do {
ans = in.nextLine();
myResp += ans;
} while (ans.indexOf("
") < 0);
wr.close();
in.close();
// System.out.println(myResp);
Pattern p = Pattern
.compile("\\s+Atlanta\\s+" \\s+
+ ""
+ "\\(ATL\\)\\s+
(.*?)"
+ "
Gate ([A-Z0-9]{2,3})\\s+"
+ "\\s+ ([A-Za-z0-9:]{6,9}) \\s+"
\\s+ (.*?)
+ "\\s+ ([a-z0-9*?:]{6,9}|[a-zA-Z\\s]{6,9}) \\s+"
\\s+ (.*?)
+ "(.*?)\\(([A-Z]{3})\\)");
Matcher m = p.matcher(myResp);
if (m.find()) {
table.put("number", flightNum);
table.put("gate", m.group(2));
table.put("scheduled", m.group(3));
table.put("estimated", m.group(5));
table.put("destination", m.group(8));
}
}
}
}
.
stackoverflow.comm
Subscribe to: Post Comments (Atom)
No comments:
Post a Comment