Scenario
Users have to login to the server with this app. Later in the app we need to check their login status against the server. We used to do this by keeping track of the last used HttpClient. Recently we switched to HttpUrlConnection but the so-called persistent connections are not working.
Question
I wrote this test app to see if the connections were persistent. I get xml back from both URLs but the connection is not behaving like it's consistent. How can I get this to work?
Note: Everything works as expected if you go to the Login url in a browser and then go to the GetUserInfo url in the same browser.
MainActivity.java
package com.mediajackagency.test;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
public Button signInBtn = null;
public Button getUserInfoBtn = null;
public TextView xmlTextView = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.xmlTextView = (TextView)findViewById(R.id.xmlTxtView);
this.signInBtn = (Button)findViewById(R.id.signInBtn);
this.signInBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AsyncTask task = new AsyncTask() {
#Override
protected Object doInBackground(Object[] params) {
String xml = loadUrl("https://www.fake.site/Login?userName=test&password=pass123");
return xml;
}
#Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
xmlTextView.setText(o.toString());
}
};
task.execute();
}
});
this.getUserInfoBtn = (Button)findViewById(R.id.getUserInfoBtn);
this.getUserInfoBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AsyncTask task = new AsyncTask() {
#Override
protected Object doInBackground(Object[] params) {
String xml = loadUrl("https://www.fake.site/GetCurrentUser");
return xml;
}
#Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
xmlTextView.setText(o.toString());
}
};
task.execute();
}
});
}
public String loadUrl(String url) {
URI uri = MainActivity.encodeUrl(url);
Log.i("XMLParser", "Get URL: " + url);
String xml = null;
URL link;
BufferedReader reader = null;
StringBuilder stringBuilder = null;
InputStream is = null;
HttpURLConnection connection = null;
try {
link = new URL(url);
connection = (HttpURLConnection) link.openConnection();
connection.setRequestMethod("GET");
connection.connect();
is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line + "\r");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
if(reader != null) reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if(is != null) is.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
if(connection != null) connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
xml = stringBuilder.toString();
} catch(Exception e) {
e.printStackTrace();
}
return xml;
}
public static URI encodeUrl(String url) {
URL urlObject;
URI uri = null;
try {
urlObject = new URL(url);
uri = new URI(urlObject.getProtocol(), urlObject.getUserInfo(), urlObject.getHost(),
urlObject.getPort(), urlObject.getPath(), urlObject.getQuery(), urlObject.getRef());
} catch (Exception e) {
e.printStackTrace();
}
return uri;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Solved With Cookies
After days / hours of working on this I found that persistent connections (http.keepalive) with HttpUrlConnection is not all it's cracked up to be:
1) You need to make sure the InputStream and HttpUrlConnection are closed before you can reuse the connection and even then it may not always be reusable.
2) Open TCP connections can be resource hogs.
After finding & testing the idea of using cookies with HttpUrlConnection I decided to go that route as it's fundamentally more sound and performs better than my original idea.
Related
Aim: Building app on Google API to fetch the data about the books the user searches
Problem Explanation:
Whenever I hit the submit Button, my app crashes.
This is my first approach in making a network request app and I need guidance.
MainActivityClass
package com.example.vidit.books;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText query = (EditText) findViewById(R.id.query);
Button submit= (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent= new Intent(MainActivity.this,Request.class);
intent.putExtra ( "text", query.getText().toString() );
startActivity(intent);
}
});
}
}
Second Class
package com.example.vidit.books;
import android.content.Intent;
public class Request {
Intent i = getIntent();
String text = i.getStringExtra ("text");
public static final String LOG_TAG = Request.class.getSimpleName();
String APIURL="https://www.googleapis.com/books/v1/volumes?q= " + text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request);
}
public void UpdateUi(Book book)
{
BookAdapter bookAdapter = new BookAdapter(this,book);
ListView listView= (ListView) findViewById(R.id.listview_all);
}
private class BookAsyncTask extends AsyncTask<URL,Void,Book>
{
#Override
protected Book doInBackground(URL... urls) {
URL url = createUrl(APIURL);
String jsonResponse = "";
try {
jsonResponse = makeHttpRequest(url);
} catch (IOException e) {
// TODO Handle the IOException
}
final Book book = extractFeatureFromJson(jsonResponse);
return book;
}
/**
* Make an HTTP request to the given URL and return a String as the response.
*/
private String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.connect();
if(urlConnection.getResponseCode()==200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
}
} catch (IOException e) {
// TODO: Handle the exception
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
// function must handle java.io.IOException here
inputStream.close();
}
}
return jsonResponse;
}
/**
* Convert the {#link InputStream} into a String which contains the
* whole JSON response from the server.
*/
private String readFromStream(InputStream inputStream) throws IOException {
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
/**
* Returns new URL object from the given string URL.
*/
private URL createUrl(String stringUrl) {
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(LOG_TAG, "Error with creating URL", exception);
return null;
}
return url;
}
private Book extractFeatureFromJson(String bookJSON) {
try {
JSONObject baseJsonResponse = new JSONObject(bookJSON);
JSONArray items = baseJsonResponse.getJSONArray("items");
// If there are results in the features array
for(int i=0;i<10;i++)
{
JSONObject firstFeature = items.getJSONObject(i);
JSONArray author=firstFeature.getJSONArray("author");
for(int j=0;j<author.length();j++)
{
JSONObject authorFeature=author.getJSONObject(j);
}
String title = items.getString(Integer.parseInt("title"));
// Create a new {#link Event} object
return new Book(title,author);
}
} catch (JSONException e) {
Log.e(LOG_TAG, "Problem parsing the earthquake JSON results", e);
}
return null;
}
}
}
BookAdapter Class:
package com.example.vidit.books;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class BookAdapter extends ArrayAdapter<Book> {
public BookAdapter(Activity context, Book book)
{
super(context,0, (List<Book>) book);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Book cbook=getItem(position);
TextView title = (TextView) listItemView.findViewById(R.id.title);
title.setText(cbook.getmTitle());
TextView author=(TextView) listItemView.findViewById(R.id.author);
author.setText((CharSequence) cbook.getmAuthor());
return listItemView;
}
}
Showing error in statement:
String text = i.getStringExtra ("text");
Need guidance
I don't know how your code gets compiled when you have overridden onCreate() in Request class and the Request class isn't extending Activity or AppCompatActivity.
Secondly, this line :
Intent i = getIntent();
String text = i.getStringExtra ("text");
should be inside the onCreate() method.
Showing error in statement : String text = i.getStringExtra ("text");
Request for Guidance
Well you need to get the data passed inside onCreate like below.
String APIURL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request);
Bundle bundle = getIntent().getExtras();
String text = bundle.getString("text");
APIURL="https://www.googleapis.com/books/v1/volumes?q= " + text;
}
And although you have the asyncTask class i can't see where exactly you execute the class. You need to do that inside onCreate as well.
Try moving this code to your onCreate method
Intent i = getIntent();
String text = i.getStringExtra ("text");
The intent extras is not available in the constructor for your Request class.
I am implementing socket programming in android. I am successfully getting data from client and displaying it to the server.
The asynctask is as follows:
public class MyClientTask extends AsyncTask<Void, Void, Void> {
String dstAddress;
int dstPort;
String response = "";
MyClientTask(String addr, int port){
dstAddress = addr;
dstPort = port;
}
#Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice:
* inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response = "IOException: " + e.toString();
}finally{
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
textResponse.setText(response);
super.onPostExecute(result);
}
}
}
The above code gets data from server and write it to the text view. I want to use the same socket to get data multiple times from server, unless a particular button is clicked. But, in doInBackground, we can't use any ui component. I want to change the following component, so that I can recieve multiple data from the server:
socket = new Socket(dstAddress, dstPort);
ByteArrayOutputStream byteArrayOutputStream =
new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int bytesRead;
InputStream inputStream = socket.getInputStream();
/*
* notice:
* inputStream.read() will block if no data return
*/
while ((bytesRead = inputStream.read(buffer)) != -1){
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
I tried to use
onProgressUpdate
but it didn't work either. Please help me to solve this.
Edit 1: the client's main activity :
package com.example.shiza.client;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "CLIENT_MESSAGE";
EditText ip_address;
EditText port_number;
EditText message_client;
Button button_send;
Button button_cancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void connect(View view) {
// ip_address = (EditText) findViewById(R.id.ip_address);
// ip_address.setText("192.168.9.100");
// port_number = (EditText) findViewById(R.id.port_number);
// port_number.setText("8080");
message_client = (EditText) findViewById(R.id.message_client);
button_send = (Button)findViewById(R.id.button_send);
button_cancel = (Button)findViewById(R.id.button_cancel);
Log.d(TAG, "connecting to the server.");
// new ConnectToServer(ip_address.getText().toString(), port_number.getText().toString(), message_client,button_send,button_cancel).execute();
new ConnectToServer("192.168.9.100","8080", message_client,button_send,button_cancel).execute();
}
}
class ConnectToServer extends AsyncTask<Void, DataOutputStream, Void> {
private static final String TAG = "CLIENT_MESSAGE";
String ip_address;
int port_number;
EditText message_client;
Button button_send;
Button button_cancel;
boolean send = false;
boolean cancel = false;
public ConnectToServer(String ip_address, String port_number, EditText message_client,Button button_send,Button button_cancel) {
this.ip_address = ip_address;
this.port_number = Integer.parseInt(port_number);
this.message_client = message_client;
this.button_cancel = button_cancel;
this.button_send = button_send;
}
#Override
protected Void doInBackground(Void... params) {
try {
Socket socket = new Socket(ip_address, port_number);
if (LoggerConfig.TAG) {
Log.d(TAG, "the socket is created at " + ip_address);
}
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
while (!cancel )
publishProgress(output);
// output.writeUTF("Hello from string");
if (LoggerConfig.TAG) {
Log.d(TAG, "I have written and closed the loop.");
}
socket.close();
} catch (IOException e) {
if (LoggerConfig.TAG) {
Log.d(TAG, "Could not connect.");
}
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(DataOutputStream... values) {
super.onProgressUpdate(values);
button_send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
send = true;
}
});
button_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel = true;
}
});
Log.d(TAG, "I am in onProgressUpdate");
if ( send )
{
try {
values[0].writeUTF(message_client.getText().toString());
Log.d(TAG, "I am in onProgressUpdate try.");
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "I am in onProgressUpdate catch.");
}
send = false;
}
}
}
The server's main activity:
package com.example.shiza.server;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
TextView ip_address;
TextView client_message;
TextView server_status;
TextView show_client_message;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ip_address = (TextView) findViewById(R.id.ip_address);
client_message = (TextView) findViewById(R.id.get_client_message);
server_status = (TextView) findViewById(R.id.server_status);
show_client_message = (TextView) findViewById(R.id.show_client_message);
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
ip_address.setText(ip);
// Making a server socket here
}
public void startServer(View view) {
GetFromClient getFromClient = new GetFromClient(this,server_status,show_client_message);
getFromClient.execute();
}
}
class GetFromClient extends AsyncTask<Void, String, Void> {
Context context;
TextView server_status;
TextView show_client_message;
String TAG = "SERVER_MESSAGE";
String inputFromClient = null;
public GetFromClient(Context context,TextView server_status,TextView show_client_message) {
this.context = context;
this.server_status = server_status;
this.show_client_message = show_client_message;
}
#Override
protected Void doInBackground(Void... params) {
Socket socket;
try {
ServerSocket serverSocket = new ServerSocket(8080);
Log.d(TAG, "Server Socket is starting....");
// server_status.setText("The server is running");
publishProgress("okay");
socket = serverSocket.accept();
DataInputStream input = new DataInputStream(socket.getInputStream());
// Calling the second background task for handling input from server
// Log.d(TAG, "Server Socket is started....");
do
{
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
inputFromClient = input.readUTF();
publishProgress(inputFromClient);
}
while ( inputFromClient != "bye" );
// publishProgress(2);
socket.close();
} catch (IOException e) {
Log.d(TAG, "I am in catch.");
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
Log.d(TAG, "I am in onProgress update.");
if ( values[0].equals("okay") )
{
server_status.setText("Server has been started");
server_status.setTextColor(context.getResources().getColor(R.color.green));
}
else
{
show_client_message.setText(values[0]);
}
}
protected void onPostExecute(Void inputFromClient)
{
Log.d(TAG, "I am in onPostExecute.");
server_status.setText("Server is not running");
server_status.setTextColor(context.getResources().getColor(R.color.red));
}
}
I am able to do the messaging but the following loop blocks everything:
do
{
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
inputFromClient = input.readUTF();
publishProgress(inputFromClient);
}
while ( inputFromClient != "bye" );
You can update your TextView in the doInBackground method using RunUiThread. After receiving the data from server just call
runOnUiThread(new Runnable() {
#Override
public void run() {
//here you update the views
}
});
me and my friend need help with a little application we're trying to create for a school project. Firstly, purpose of this application is finding pharmacy around where we live. These two are the sites that I'm trying to pull information from (is this called pulling?)
http://www.istanbulsaglik.gov.tr/w/nobet/liste.asp?lc=0&gun=09.05.2015
gun = day/date
lc = id of town
cs is given to a town daily and changes daily, it's just a number.
in this page.
AND/OR
http://www.nobetcieczanebul.com/
this one 'looks' much more easier to obtain required info. from.
They both contain the info that I need; name of the pharmacy, it's address and maybe phone numbers. By using JSOUP I'm hoping to parse (pull?) those information but I need help. My first priority is using 'webview'.
First we'll read cs from HTML, after that we'll put the cs into the code and call all the names, dates and addresses of pharmacy stores.
Here's my example code, this one calls id 32 town of date 05/05/2015 which contains name etc.
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
Button btnGetir;
ProgressBar pb;
TextView tv;
Button btnNobet;
TextView tvNobet;
WebView wv;
class mytask extends AsyncTask<Void,Void,Void>{
String Title;
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pb.setVisibility(View.INVISIBLE);
tv.setText(Title);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params) {
try {
Document myDoc= Jsoup.connect("http://www.ticaret.edu.tr").get();
Title=myDoc.title();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
class myNobettask extends AsyncTask<Void,Void,Void>{
String Title;
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
pb.setVisibility(View.INVISIBLE);
wv.loadData(Title, "text/html", "UTF-8");
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pb.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params) {
try {
Document myDoc= Jsoup.connect("http://apps.istanbulsaglik.gov.tr/eczane/GetNobetciEczaneler.aspx?lc=32&gun=05.05.2015&cs=6e79f453").get();
Title=myDoc.html();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGetir=(Button) findViewById(R.id.btnTitleGetir);
tv=(TextView)findViewById(R.id.tvSonuc);
pb=(ProgressBar) findViewById(R.id.progressBar);
btnNobet =(Button)findViewById(R.id.btnNobetci);
tvNobet=(TextView)findViewById(R.id.tvNobetci);
wv=(WebView) findViewById(R.id.webView);
btnGetir.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
mytask newtask= new mytask();
newtask.execute();
}
});
btnNobet.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
myNobettask task= new myNobettask();
task.execute();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
With JSoup you should be able to specify which CSS selectors/ HTML components you wish to parse.
Document doc = Jsoup.connect(url).get();
Elements links = doc.select("a[href]"); // This should return a collection of links.
It is possible to specify further so perhaps you wish to return a subset of all paragraph tags, you could then specify:
Document doc = Jsoup.connect(url).get();
Elements linksInPTags= doc.select("p > a[href]");
Read through the Jsoup cookbook. It's pretty clear and really helpful.
Here's a snippet of code using Google Places API that may extremely helps :
public static void makeRequest(String url)
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
instream.close();
}
} catch (Exception e) {}
}
private String convertStreamToString(InputStream is) {
BufferedReader.readLine()
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
Steps to accomplish :
1 - You have to generate URL which performs your need ( as an example : https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=AddYourOwnKeyHere )
( a full description of creating the URL exists in the link above )
2 - It's optional but more reliable to use this method in a background thread
3 - You have to parse the response ( response can be in json or xml format you can choose it from the URL ) data to perform your needs ( you can get response fields description from the link above )
If I run my app in the emulator my app crashes immediately. I get the error 'Caused by: java.lang.IllegalArgumentException: host=null, port=4444.' Logcat says the error comes from InetSocketAdress. This is my server code:
package com.imptmd.charliemacdonald.desleutelaar_v3;
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
/**
* Created by Charlie on 26-3-2015.
*/
public class Server extends AsyncTask<Void, Void, String> {
private String message;
private String ip;
public static int port = 4444;
private String serverResponse = null;
public Server(String ip, int port, String message ) {
super();
//IP, Port en bericht om naar server te sturen
this.message = message;
this.ip = ip;
this.port = port;
}
#Override
protected String doInBackground(Void... params) {
try {
Socket serverSocket = new Socket();
serverSocket.connect(new InetSocketAddress(this.ip, this.port), 4444);
this.sendMessage(message, serverSocket);
InputStream input;
try {
input = serverSocket.getInputStream();
BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(input));
String line = "";
StringBuilder stringBuilder = new StringBuilder();
while ((line = responseStreamReader.readLine()) != null) {
stringBuilder.append(line);
}
responseStreamReader.close();
this.serverResponse = stringBuilder.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Response: " + serverResponse);
} catch (UnknownHostException e) {
Log.d("debug", "can't find host");
} catch (SocketTimeoutException e) {
Log.d("debug", "time-out");
} catch (IOException e) {
e.printStackTrace();
}
return serverResponse;
}
private void sendMessage(String message, Socket serverSocket) {
OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(serverSocket.getOutputStream());
} catch (IOException e2) {
e2.printStackTrace();
}
if (outputStreamWriter != null) {
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
PrintWriter writer = new PrintWriter(bufferedWriter, true);
writer.println(message);
}
}
}
The code of the class where the user can fill in the IP:
package com.imptmd.charliemacdonald.desleutelaar_v3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.concurrent.ExecutionException;
public class GebruikerIP extends Activity {
private Boolean serverCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTitle("Slotenbedrijf De Sleutelaar");
//Check voor internet verbinding
if(NetwerkCheck.isInternetAvailable(GebruikerIP.this))
{
}
else
{
Toast.makeText(GebruikerIP.this, "Er is helaas geen internetverbinding geconstateerd, daarom wordt nu de laatst opgehaalde informatie getoond.!", Toast.LENGTH_LONG).show();
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gebruikerip);
Button ipButton = (Button) findViewById(R.id.serverbutton);
ipButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
verbindServer();
}
});
//Enter key afvangen
EditText ipInvoer = (EditText) findViewById(R.id.ipinvoeren);
ipInvoer.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_ENTER:
verbindServer();
break;
default:
return false;
}
return true;
}
});
}
//server connectie maken voor ophalen van diensten
public void verbindServer() {
TextView ipVeld = (TextView) findViewById(R.id.ipinvoeren);
String ip = ipVeld.getText().toString();
Log.i("ip", ip);
String response = null;
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("slotenlijst", "");
} catch (JSONException e) {
e.printStackTrace();
}
try {
try {
response = new Server(ip,
4444, jsonObject.toString()).execute().get();
//exceptions afvangen
} catch (InterruptedException e)
{
}
} catch (ExecutionException e1)
{
}
if (response == null) {
serverCheck = false;
Toast.makeText(this, "Verbinden met server mislukt, staat server aan?", Toast.LENGTH_LONG).show();
} else {
//doorgaan naar MainActivity
serverCheck = true;
HoofdschermFragment.serverIp = ip;
Intent startApp = new Intent(this, MainActivity.class);
startActivity(startApp);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_gebruiker_ip, 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();
return super.onOptionsItemSelected(item);
}
}
I can't find out how I can fix this error. Help would me appreciated. Thanks!
change it to
InetAddress inetServer = InetAddress.getByName(IP);
than
Socket socket = new Socket(inetServer, devicePort);
and if you are using for Server Socket than use below line
ServerSocket socket = new ServerSocket(devicePort,int backlognumber, inetServer);
I have a method that should actually read the data from the web and returns the content as a String, but it is not working.I would be glad if you can help me out! I don't know where I screwed up. I added the INTERNET uses permission in the manifest file but doesn't work. Well, I tested on the emulator only.
public String getInternetData() throws Exception {
BufferedReader in = null;// read info
String response = null;
try {
HttpClient client = new DefaultHttpClient(); // default client
// process application to handle data from web
URI website = new URI("https://www.random.com");
// get information from web
HttpGet request = new HttpGet(); // get data
InputStream inputStream = null;
request.setURI(website);// connected and request
// respons
HttpResponse httpResponse = client.execute(request);
int statutCode = httpResponse.getStatusLine().getStatusCode();
int length = (int) httpResponse.getEntity().getContentLength();
inputStream = httpResponse.getEntity().getContent();
Reader reader = new InputStreamReader(inputStream, "UTF-8");
int inChar;
StringBuffer stringBuffer = new StringBuffer();
while ((inChar = reader.read()) != -1) {
stringBuffer.append((char) inChar);
}
response = stringBuffer.toString();
} finally {
}
return response;
}
I checked your code and it's working. I'm not sure if you are establishing a connection on your EDT or not but here's a template that's working with your code. Btw your link doesn't have a SSL, you need to connect to http.
package com.example.stackover;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
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.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
(new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
getInternetData();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}).execute();
return rootView;
}
public void getInternetData() throws Exception {
BufferedReader in = null;// read info
String response = null;
try {
HttpClient client = new DefaultHttpClient(); // default client
// process application to handle data from web
URI website = new URI("http://www.random.com");
// get information from web
HttpGet request = new HttpGet(); // get data
InputStream inputStream = null;
request.setURI(website);// connected and request
// respons
HttpResponse httpResponse = client.execute(request);
int statutCode = httpResponse.getStatusLine().getStatusCode();
int length = (int) httpResponse.getEntity().getContentLength();
inputStream = httpResponse.getEntity().getContent();
Reader reader = new InputStreamReader(inputStream, "UTF-8");
int inChar;
StringBuffer stringBuffer = new StringBuffer();
while ((inChar = reader.read()) != -1) {
stringBuffer.append((char) inChar);
}
response = stringBuffer.toString();
} finally {
}
Log.v("Information", response);
}
}
}