Write data to a Text file on URL in Android - android

I am writing an Android Application in Eclipse in which i need to write some text to a Text file on my webserver. In the onCreate() I am writing something like this..
try{
String data = "hello";
URL u1 = new URL("http://url.com/script.txt");
URLConnection uc1 = u1.openConnection();
uc1.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(uc1.getOutputStream());
out.write(data);
out.flush();
}
catch( Exception e ) {
e.printStackTrace();
}
But its not working. I just need to write a simple text onto an existing Text file on my Web Server. I know its a simple task but I have tried several ways and spent sometime on google trying to do this but not working..Some times I receive errors in yellow:
05-16 16:22:24.853: W/System.err(29095): android.os.NetworkOnMainThreadException
05-16 16:22:25.563: W/System.err(29095): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
...
...
...
I tried writing in Async also:
private class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
ProgressDialog mProgressDialog;
#Override
protected void onPostExecute(Void result) {
}
#Override
protected void onPreExecute() {
}
#Override
protected Void doInBackground(Void... params) {
//code comes here.....
}
}
But nothing is working.. i have android.permission.ACCESS_NETWORK_STATE and android.permission.INTERNET enabled in manifest. My folders in server hav write permissions.
Any help is appreciated... Thanks

You cannot access file served by some server like they resources on your file system.
Use HTTP POST with data and have the file writing done by some script engine like PHP.
The use of AsyncTask is required as well.

This is an issue with implementation on your webserver, not Android. One does not simply write over HTTP. You need something like a REST API or a specialized protocol.
Write permissions on the server only mean that the current user on the server has permissions to write to those folders. Outside clients still can only read unless you have something like a PHP script that accepts a POST variable with the text you want to write.

Related

i want use jsoup to grab news on my android app

I have a problem that I want to use jsoup to grab news but always fail.
this is news website.
https://www3.nhk.or.jp/news/
this is my picture . which I circle is I wanted data.
https://drive.google.com/open?id=1KJAyOSdHO8APPD6_A9MjxkoFjekcQLXt
but no matter what I do. it always get wrong data or empty.
this is my program.
public class News extends AppCompatActivity {
Button ok;
private static final String url ="https://www3.nhk.or.jp/news/";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
ok=(Button)findViewById(R.id.ok);
ok.setOnClickListener(okbtn);
}
private Button.OnClickListener okbtn=new Button.OnClickListener(){
public void onClick(View v){
try{
Connection.Response response = Jsoup.connect(url).execute();
String body = response.body();
Document data = Jsoup.parse(body);//visible-phone print_hide
Elements country=data.select("main");
Elements main=data.select("div[id=module module--news-main index-main]");
for(Element e1: country)
{
mytoast(e1+"");
}
}
catch(Exception ex){ex.printStackTrace() ;}
}
};
private void mytoast(String str)
{
Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
please help me
thanks
You can try to see it's HTML first.
If you can't see it, you don't use jsoup.
There's a small hint in its comment:
このページではJavaScriptを使用しています
=>This is generated by JavaScript
If it's generated, you can't find it from Jsoup.
In this case, I'll use Chrome's tool to monitor the XHR tab
Look into each XHR request, and find the most possible one,
for example, I see this
https://www3.nhk.or.jp/news/json16/syuyo.json?_=1559183885640
A part of the response:
"id":"193411",
"title":"三菱UFJ銀行 新規口座は原則デジタル通帳に",
"pubDate":"Thu, 30 May 2019 04:03:11 +0900",
"cate":"5",
...
"id":"193437",
"title":"エアレース世界選手権 今季限りで終了",
"pubDate":"Thu, 30 May 2019 09:40:37 +0900",
So this is exactly what you want. It comes from another link!
You don't need Jsoup, just HttpGet the link
https://www3.nhk.or.jp/news/json16/syuyo.json?_=1559183885640
And I think the numbers looks like UnixTime,
So I check the current time is : 1559184830782, that's it.
Just use that link as API and time as parameter.

LibGDX http request not working

I want to send a String message to database when user presses a specific button in the LibGDX game I am designing for android. How do I go about doing that? Following is the code I tried. But it does not work.
Net.HttpRequest httpRequest = new Net.HttpRequest();
httpRequest.setMethod("POST");
httpRequest.setUrl("URL is here");
httpRequest.setContent("INSERT INTO `game_table` (`Button`) VALUES ('Button 1 Pressed')");
Net.HttpResponseListener httpResponseListener = new Net.HttpResponseListener() {
#Override
public void handleHttpResponse(Net.HttpResponse httpResponse) {
Gdx.app.log("Log httpResponse", httpResponse.getResultAsString());
}
#Override
public void failed(Throwable t) {
}
#Override
public void cancelled() {
}
};
Gdx.net.sendHttpRequest(httpRequest,httpResponseListener);
Log does not provide anything in android monitor. I also tried using AsyncTask and without AsyncTask to implement this code. But neither works.
Am I missing something? If so could you give me small code snippet that will work?
You don't need to use an AsyncTask, libGDX' HTTPRequest is async out of the box.
You did not log anything if the request fails or is cancelled so probably that's the case.

Android App has stopped working when getting html page with Jsoup

I am a newbie to Android App Development. My android app shows "Unfortunately, AppTest has stopped." when I try to connect to Internet with Jsoup with the following code.
I have already added Internet permission to AndroidManifest.xml.
Jsoup is working correctly when I test with just offline html such as <p>Nay</p>.
My Android phone is completely available Internet Connection. What is wrong?
Document doc;
try {
// need http protocol
doc = Jsoup.connect("http://google.com").get();
// get page title
String title = doc.title();
psi.setText(title);
} catch (IOException e) {
e.printStackTrace();
}
Thanks to all, but the answers and suggestions could not help me very much. After doing a lot of mistakes, I have found the solution. I would like to post my answer to help other people when meet this problem.
Jsoup seems to run in another thread than UI thread. Therefore, you just need to create another thread with AsyncTask to use Jsoup like the following.
class GetHtmlwithJsoup extends AsyncTask<String, Integer, String>
protected String doInBackground(String... param) {
try {
//Jsoup Connecting and Getting html page code at here
}
catch (TwitterException e) {
return "Failed to get";
}
}
#Override
protected void onProgressUpdate(Integer... values) { //
super.onProgressUpdate(values);
// Not used in my case
}
#Override
protected void onPostExecute(String result) { //
//show Jsoup HTML Ouput to UI widgets code at here...
}
}
If you use doc outside this block, you must check if it was properly initialized.
Other guess, does your app request the INTERNET permission?

Controlling GoPro with URL commands

I have a GoPro Hero3 Black Edition and after reading their user forums, I got these 2 url commands that can control the shutter button while the GoPro is acting as a hotspot.
Record/shoot Command
On http://10.5.5.9:80/bacpac/SH?t=WIFIPASSWORD&p=%01
Off http://10.5.5.9:80/bacpac/SH?t=WIFIPASSWORD&p=%00
I have tried using the URLs in my Nexus 7's Chrome Browser but I want to integrate these 2 commands in a button in my Android app when my Nexus 7 connects via wifi to the GoPro.
How do I do this? Thanks in advance.
It's not that tough. Create an activity class and a couple of buttons to fire the HTTP commands.
Keep in mind that these are network calls and have to be made from a separate background thread (not the main thread).
btnToggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Toggle the camera power
new OperateCamera().execute();
}
});
Create a new AsyncTask class:
class OperateCamera extends AsyncTask<String, Void, Boolean> {
protected Boolean doInBackground(String... urls) {
return triggerShutter();
}
// Method to trigger the shutter
boolean triggerShutter(){
try {
// Make network call
return true;
}
catch (Exception e) {
return false;
}
}
}
Just use the legacy URLConnection or some lib like OkHttp to access these urls and trigger the shutter. I'd recommend the second ;)

Android/Java: How do i implement AsyncTask?

I've made an app that sends a request to a webserver in a specified interval and gets XML data. It then parses the XML data, gets information from the phone (text messages, contacts or something similar) and shoots it back to the server with a http post request.
The problem is that it usually takes a few seconds for the app to get the info, which often leaves the app crashing. A dialog comes up saying the app has become unresponsive and asks if i want to close the app or wait, if i press wait it eventually starts working again.
Is AsyncTask the right solution to this problem?
Another thing i don't really understand is how AsyncTask actually works. Let's say i have two methods that do a lot of work and crashes the app, can i put both of them in one AsyncTask and just call them from doInBackground()?
I have also implemented something similar you are trying. That is sending request to server, receive XML response, parse XML, show result. View This. I have used AsyncTask for this.
Here is how i have implemented it using AsynTask
private class AsyncClass extends AsyncTask<Void, Void, Bundle>{
#Override
protected Bundle doInBackground(Void... arg0) {
Bundle b=startProcess();
// startBundle() method do all the processing and return result in a bundle. You can as many methods from within startBundle() method
return b;
}
#Override
protected void onPostExecute(Bundle result) {
Log.d(TAG , "In onPostExecute");
dialog.dismiss();
if(result==null)
Toast.makeText(cont, "Can't process query.\nTry again later.", Toast.LENGTH_LONG).show();
else{
Intent in = new Intent(cont, QueryResultDisplay.class);
Log.d(TAG , "Displaying");
in.putExtras(result);
cont.startActivity(in);
}
}
I give you brief description about your problem.
There are many possibility that you don't get data from server
if your network speed is very slow and you try to get all the
information from server and XML data then in this case if network crash then it show you error
if you're making request to that page which is not in server
Now, if you are facing the problem in code, then I give you the complete code of AsyncTask class which I had implemented in my project and it work fine.
private class GetLoginResponse extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog progressDialog;
private String email;
private String password;
public GetLoginResponse(String emailId, String paswd) {
this.email = emailId;
this.password = paswd;
}
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(LoginActivity.this, "",
"Loading....", true, false);
}
#Override
protected Boolean doInBackground(Void... params) {
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpclient.execute(httpGet);
//here u can check the reponse is ok and 200
} catch (NetworkException e) {
isNetworkError = true;
}
return false;
}
#Override
protected void onPostExecute(Boolean data) {
progressDialog.dismiss();
System.out.println("lOGIN RESPONSE for email = " + email + data);
}
}// end AsyncTask
This would solve your problem.
Yes, you can use AsyncTask.
The code called in doInBackground() must not touch the UI.
You can touch the UI thread with publishProgress().

Categories

Resources