When trying to add words to a database via php, only the first word of both strings gets added.
I send the text via this code:
public void sendTextToDB() {
valcom = editText1.getText().toString();
valnm = editText2.getText().toString();
t = new Thread() {
public void run() {
try {
url = new URL("http://10.0.2.2/HB/hikebuddy.php?function=setcomm&comment="+valcom+"&name="+valnm);
h = (HttpURLConnection)url.openConnection();
if( h.getResponseCode() == HttpURLConnection.HTTP_OK){
is = h.getInputStream();
}else{
is = h.getErrorStream();
}
h.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Test", "CONNECTION FAILED 1");
}
}
};
t.start();
}
When tested with spaces and commas etc. in a browser, the php function adds all text.
The strings also return the full value when inserted into a dialog.
How do I fix this?
Thank you.
You need to URL-encode valcom and valnm when putting them into the URL.
See java.net.URLEncoder.encode: http://developer.android.com/reference/java/net/URLEncoder.html
url = new URL("http://10.0.2.2/HB/hikebuddy.php?function=setcomm&comment="
+ URLEncoder.encode(valcom)
+ "&name="+ URLEncoder.encode(valnm));
Related
I am working on Android. I want to extract comments from a web page using jsoup library. I am doing in this way. But could not do that. Can anyone help?
public void fun() {
Document doc = null;
try {
doc = Jsoup.connect("http://tribune.com.pk/story/1164751/federal-govt-dodged-chinese-govt-cpec/").timeout(10 * 1000).get();
} catch (IOException e) {
e.printStackTrace();
}
Elements pa = doc.getElementsByClass("span-12 last");
int count = 1;
for (Element iter : pa) {
System.out.println( iter.text());
count = count + 1;
}
}
You have 2 issues here:
Your program closes because the server expects to get a userAgent string and returns you a 403 error.
The comments are located under the "li-comment" class.
This code works for me:
Document doc = null;
try {
doc = Jsoup.connect("http://tribune.com.pk/story/1164751/federal-govt-dodged-chinese-govt-cpec/").timeout(10 * 1000)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
.get();
} catch (IOException e) {
e.printStackTrace();
}
Elements el = doc.getElementsByClass("li-comment");
for (Element e : el) {
System.out.println(e.text());
System.out.println("-----------------");
}
You should also handle the case that li-comment is emtpy or does not exist, in case that there are no comments on the page.
on button click i used this..
public void fetchData(View v) {
Toast.makeText(getApplicationContext(),
"Data is fetching from The Hindu wait some time ",
Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
#Override
public void run() {
try {
// get the Document object from the site. Enter the link of
// site you want to fetch
/*
* Document document = Jsoup.connect(
* "http://javalanguageprogramming.blogspot.in/") .get();
*/
Document document = Jsoup.connect(
"http://www.thehindu.com/").get();
title = document.text().toString();
// Get the title of blog using title tag
/* title = document.select("h1.title").text().toString(); */
// set the title of text view
// Get all the elements with h3 tag and has attribute
// a[href]
/*
* Elements elements = document.select("div.post-outer")
* .select("h3").select("a[href]"); int length =
* elements.size();
*/
Elements elements = document.select("div.fltrt")
.select("h3").select("a[href]");
int length = elements.size();
for (int i = 0; i < length; i++) {
// store each post heading in the string
posts += elements.get(i).text();
}
// Run this on ui thread because another thread cannot touch
// the views of main thread
runOnUiThread(new Runnable() {
#Override
public void run() {
// set both the text views
titleText.setText(title);
postText.setText(posts);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
I have list of web pages(over 100) with I have to vistit and collect data from.
I decided to save the html from all of them to one file, and then use Jsoup to find the interesting data.
But problem is to I do not know how to run 100 threads, and save the responses into one file, any ideas?
maybe it's not a masterpiece, but it works, and I wanted to make it as simple as possible.
ArrayList<String> links = new ArrayList<>();
Elements myDiv;
private void saveDetails() throws IOException {
if(repeat < links.size()){
repeat++;
textView.setText(String.valueOf(repeat));
saveFile(myDiv.toString());
myDiv = null;
getDetails(links.get(repeat));
}else {
textView.setText("finished");
}
}
private void getDetails(String urlStr) {
final String detailsUrl = urlStr;
new Thread() {
#Override
public void run() {
Message msg = Message.obtain();
try {
Document doc = Jsoup.connect(detailsUrl).get();
myDiv = doc.select(".exhibitor-contact");
} catch (IOException e1) {
e1.printStackTrace();
}
detailsHandler.sendMessage(msg);
}
}.start();
}
private Handler detailsHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
try {
saveDetails();
} catch (IOException e) {
e.printStackTrace();
}
}
};
You don't need to save all of them in a file and then process them. You can gather information one by one. It is my suggestion:
arrayList urls = {100 site-url}; //in correct syntax
Document doc = null;
for (String url : urls) {
doc = Jsoup.connect(url).get();
//now proccess doc.toString as you want(in regular expression for example)
//save your desired information
}
I am trying to develop a function that gives my android application a test to see if the data on the phone matches that on the server.
I have every part of the function working fine apart from I want the message to come back from the server to the handler then I want the handler to return false or true and pass the value to function which returns a boolean.
A point in the right direction would be greatly appreciated.
here is the android code so far.
public boolean isTripUpladedToServer(int tripId)
{
if(isServiceRunning()&&tripId==currentTripId){return false;}
SQLiteDatabase db;
db=this.openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.OPEN_READWRITE, null);
String Qu="SELECT COUNT(tripid) from TRIP_DATA WHERE TRIPID="+tripId+";";
Cursor c= db.rawQuery(Qu, null);
int count=0;
if(c!=null &&c.moveToFirst())
{
count=c.getInt(0);
}
JSONArray parcel =new JSONArray();
JSONObject header =new JSONObject();
JSONObject message =new JSONObject();
try {
header.put("tablename", "isTripUploaded");
header.put("userid", userid);
parcel.put(header);
message.put("count", count);
message.put("tripid", tripId);
parcel.put(message);
Log.i(tag, parcel.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Handler inner=new Handler()
{
#Override
public void handleMessage(Message msg)
{
try {
JSONObject ret=new JSONObject(msg.obj.toString());
Log.i(tag,ret.toString());
// I want the function to return the boolean value that the server has sent to phone.
} catch (JSONException e) {
e.printStackTrace();
}
}
};
new uploadero(inner).execute(parcel);
//the below return value is here to prevent the error, ideally I want to remove it
return false;
}
If I have approached this in the wrong way please say, thanks in advance Mark
Use a class variable for the boolean and use a getter in the handler to get it.
I have google alot, read javadoc, plus search different forums including this reading the issue but not found the correct answer to my question. The code snippet below is working fine but I want to exactly know what function to use exactly for read/write file in android. One can write to internal storage using OutputStream, FileOutputSteam.write(), Other is to use OutputStreamWriter(FileOutputSteam).write(), further BufferedWriter(OutputStreamWriter).write(), and finally PrintWriter.write().
Same goes for the InputStream case whether to use InputStream, FileInputSteam.read(), InputSreamReader(FileInputStream).read(), BufferedReader(InputStreamReader).
I want to know which exactly is the best proper way to do it. Please help me out as totally confused with this.
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getName();
private static final String FILENAME = "students.txt";
private EditText stdId;
private Button Insert;
private Button Search;
private Button Update;
private Button Delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String TAG = MainActivity.class.getName(); //output: com.fyp2.testapp.MainActivity
//value used for insert/delete/search
stdId = (EditText) findViewById(R.id.editTxtId);
//insert value in application sandbox file
Insert = (Button) findViewById(R.id.btnInsert);
Insert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String IdToInsert = stdId.getText().toString().trim();
if(IdToInsert.length() > 0) {
myInsertFunc(IdToInsert);
}
else {
Toast.makeText(getApplicationContext(), "Id cannot be null!", Toast.LENGTH_SHORT).show();
stdId.requestFocus();
}
}
});
//search value from application sandbox file
Search = (Button) findViewById(R.id.btnSearch);
Search.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String IdToSearch = stdId.getText().toString().trim();
if(IdToSearch.length() > 0) {
mySearchFunc(IdToSearch);
}
else {
Toast.makeText(getApplicationContext(), "Id cannot be null!", Toast.LENGTH_SHORT).show();
stdId.requestFocus();
}
}
});
//delete value from application sandbox file
Delete = (Button) findViewById(R.id.btnDelete);
Delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String IdToDelete = stdId.getText().toString().trim();
if(IdToDelete.length() > 0) {
myDeleteFunc(IdToDelete);
}
else {
Toast.makeText(getApplicationContext(), "Id cannot be null!", Toast.LENGTH_SHORT).show();
stdId.requestFocus();
}
}
});
}
//function to insert
private void myInsertFunc(String data) {
//If student id already exist don't write it again -> Not handled at the moment
//Other exceptions may not have been handled properly
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(FILENAME, Context.MODE_APPEND));
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
bufferedWriter.append(data);
bufferedWriter.newLine();
Toast.makeText(getApplicationContext(), "Student ID: " + data + " Inserted Successfully!", Toast.LENGTH_SHORT).show();
bufferedWriter.close();
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
}
//function to search
private void mySearchFunc(String data) {
//Id id not found show toast to user -> Not handled at the moment
try {
InputStream inputStream = openFileInput(FILENAME);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
while ((receiveString = bufferedReader.readLine()) != null) {
if(receiveString.contains(data))
{
Toast.makeText(getApplicationContext(), "Found Student ID: " + data , Toast.LENGTH_SHORT).show();
break;
}
}
bufferedReader.close();
inputStream.close();
}
}
catch (FileNotFoundException e) {
Log.e(TAG, "File not found: " + e.toString());
} catch (IOException e) {
Log.e(TAG, "Can not read file: " + e.toString());
}
}
private void myDeleteFunc(String data) {
/* I have found a solution to delete a specific line from the file.
* But the problem is that it needs to scan the whole file line by line and copy the file contents that not matches the string to temp file.
* This solution can reduce the effeciency. Consider search 20,000 records in a file.
* Need to work around on it.
*/
}
private void myUpdateFunc(String data) {
/* Same goes to the update process...
* Need to scan all records and copy content in temp file and put the updated data in that file.
* Need to work around on this issue too...
*/
}
#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;
}
}
They all serve a purpose. There is best proper way, because each writer was made for a reason. Usually anything that has buffered is more efficient if you have mulitple read/writes/flush.
Both the Android and Java docs can be helpful and java docs give you more description for these writers. Check out these documentation. Try reading the javase6 ones which are more detailed:
Android_FileWriter
Java_FileWriter
Android_OutputStreamWriter
Java_OutputStreamWriter
Android_BufferedWriter
Java_BufferedWriter
When my app is started, I get tiket from server that I use for sign response to server. This ticket (string) I save in database and when I run new response I get this tiket from database and attach to response. All was fine when I run responses from same activity where I save my ticket. I mean that when I am saving tiket, I also releases context of activity where I am executing this actions with database. BUT when I save tiket in one activity, launch another activity and try to get ticket from database in second activity, I get null (rown didn't contain item). So, can anybody tell me, where I'm erred?
I appreciate any help. Thank you.
Here code:
public long setTicket(Ticket ticket) throws SQLException {
ContentValues insertValues = new ContentValues();
insertValues.put(TicketTable.KEY_TICKET, ticket.getTicket());
insertValues.put(TicketTable.KEY_RESULT_CODE, ticket.getResultCode());
long result = m_db.insert(DATABASE_TABLE_TICKET, null, insertValues);
return result;
}
public Ticket getTicket() throws SQLException {
String[] columns = { TicketTable.KEY_ID, TicketTable.KEY_TICKET,
TicketTable.KEY_RESULT_CODE };
Cursor cursor = getCursor(DATABASE_TABLE_TICKET, columns);
Ticket result = new Ticket();
result.setTicket(cursor.getString(TicketTable.TICKET_COLUMN));
result.setResultCode(cursor.getString(TicketTable.RESULT_CODE_COLUMN));
cursor.close();
return result;
}
Here methods where I set access to database:
public String getTicket(){
DBAdapter adapter = new DBAdapter(m_context);
adapter.open();
String ticketEncoded = null;
Ticket ticket = null;
try {
ticket = adapter.getTicket();
ticketEncoded = java.net.URLEncoder.encode(ticket.getTicket());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
adapter.close();
}
return ticketEncoded;
}
public void saveTicketFromServer(){
GetTicketHandler handler = new GetTicketHandler(m_context);
Ticket ticket = new Ticket();
ticket.setCommand("getTicket");
TicketTask task = new TicketTask(ticket);
createTask(task, handler);
}
And here I have caught null insted of ticket:
String comment = java.net.URLEncoder.encode(strToEncode, "UTF-8");
String barcodeId = "4605246006340";
String ball = "10";
String sign = account.getTicket();//here!
CreateCommentTask task = new CreateCommentTask(barcodeId, ball,
comment, sign);
//TODO create handler
HttpService httpService = new HttpService();
httpService.createTask(task, null);
But in another response all fine:
String sign = account.getTicket();
try {
task = new SearchBarcodeTask(searchForm, sign);
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SearchBarcodeHandler handler = new SearchBarcodeHandler(m_activity);
HttpService httpService = new HttpService();
httpService.createTask(task, handler);
Alright the problem that I see is that String sign = account.getTicket();//here!
The account has to be null. So check what you are doing differently between the two other method calls you mentioned. You are doing something different with whatever account is.