Phantom
(Jishnu)
July 20, 2021, 4:16am
#1
Phantom
(Jishnu)
July 20, 2021, 4:54am
#4
After looking at some websites in Google, I have a few solutions without using dependencies.
Using StringBuilder
public String convertArrayToCsv(YailList list) {
Object[] array = list.toArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
sb = sb.append(array[i]);
if(i != array.length - 1) {
sb.append(",");
}
}
return sb.toString();
}
toString (not recommended)
public String convertArrayToCsv(YailList …
I Tried It I Is Not Working
Phantom
(Jishnu)
July 20, 2021, 4:55am
#5
And I Need Help With Posting In Web In JAVA
String sql = "something";
String query = "something";
List a = Arrays.asList("Context-Type", "Application/x-www-form-urlencoded");
List b = Arrays.asList(a);
Web web = new Web();
web.Url("your url");
web.RequestHeaders(YailList.makeList(b));
List key = Arrays.asList("key", web.UriEncode(sql));
List queryy = Arrays.asList("query", web.UriEncode(query));
List requestData = Arrays.asList(key, queryy);
web.PostText(web.BuildRequestData(requestData));
you have to import -
com.google.appinventor.com…
I Tried It But I Don’t Know How Read The Response
vknow360
(Sunny Gupta)
July 20, 2021, 5:16am
#6
Good luck with your journey.
2 Likes
Phantom
(Jishnu)
July 20, 2021, 5:24am
#7
can you help me out with this
Phantom
(Jishnu)
July 20, 2021, 6:05am
#9
@Xoma Started Compiling Project WebPost
Buildfile: /compiler/android/build.xml
javac:
[mkdir] Created dir: /compiler/android/build/dPXpo/classes
[javac] Compiling 1 source file to /compiler/android/build/dPXpo/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/simple_components.json
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/simple_components.txt
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/simple_components_build_info.json
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/AutogeneratedOdeMessages.java
[javac] Note: Wrote file file:/compiler/android/build/dPXpo/classes/ComponentsTranslation.java
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:46: error: unreported exception MalformedURLException; must be caught or declared to be thrown
[javac] URL obj = new URL(“https://www.google.com ”);
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:47: error: unreported exception IOException; must be caught or declared to be thrown
[javac] HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:49: error: unreported exception ProtocolException; must be caught or declared to be thrown
[javac] httpConnection.setRequestMethod(“POST”); //request method
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:51: error: unreported exception IOException; must be caught or declared to be thrown
[javac] if (httpConnection.getResponseCode() == 200) {
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:52: error: unreported exception IOException; must be caught or declared to be thrown
[javac] bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:58: error: unreported exception IOException; must be caught or declared to be thrown
[javac] while ((line = bufferedReader.readLine()) != null){
[javac] ^
[javac] /compiler/android/src/dPXpo/com/web/post/WebPost.java:61: error: unreported exception IOException; must be caught or declared to be thrown
[javac] bufferedReader.close();
[javac] ^
[javac] 7 errors
Xoma
(Kumaraswamy)
July 20, 2021, 6:06am
#10
Use try-catch or add an IO Exception.
Phantom
(Jishnu)
July 20, 2021, 6:24am
#11
Xoma:
try-catch
@SimpleFunction(description = "b")
public void TestFunction(){ try
URL obj = new URL("https://www.google.com");
HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
httpConnection.setDoOutput(true); //if you want output
httpConnection.setRequestMethod("POST"); //request method
BufferedReader bufferedReader = null;
if (httpConnection.getResponseCode() == 200) {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
} else {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
}
StringBuilder content = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
content.append(line).append("\n");
}
bufferedReader.close();
final String con = String.valueOf(content); //post wherever you need
Imports
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
Error
Started Compiling Project WebPost
Buildfile: /compiler/android/build.xml
javac:
[mkdir] Created dir: /compiler/android/build/oQpjH/classes
[javac] Compiling 1 source file to /compiler/android/build/oQpjH/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/oQpjH/com/web/post/WebPost.java:45: error: ‘{’ expected
[javac] public void TestFunction(){ try
[javac] ^
[javac] /compiler/android/src/oQpjH/com/web/post/WebPost.java:45: error: ‘try’ without ‘catch’, ‘finally’ or resource declarations
[javac] public void TestFunction(){ try
[javac] ^
[javac] /compiler/android/src/oQpjH/com/web/post/WebPost.java:65: error: reached end of file while parsing
[javac] }
[javac] ^
[javac] 3 errors
[javac] 1 warning
Phantom
(Jishnu)
July 20, 2021, 6:27am
#12
full code
package com.web.post;
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
public class WebPost extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
public WebPost(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleFunction(description = "b")
public void TestFunction(){ try-catch
URL obj = new URL("https://www.google.com");
HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
httpConnection.setDoOutput(true); //if you want output
httpConnection.setRequestMethod("POST"); //request method
BufferedReader bufferedReader = null;
if (httpConnection.getResponseCode() == 200) {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
} else {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
}
StringBuilder content = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
content.append(line).append("\n");
}
bufferedReader.close();
final String con = String.valueOf(content); //post wherever you need
}
}
Phantom
(Jishnu)
July 20, 2021, 6:29am
#13
@Xoma what should i do now
Phantom:
{ try-catch
This is not the right method
try{}
catch{}
Developers need to use it to catch Any error
Like This
try{your code to run}
catch(Exception e){
e.printStackTrace();
}
try is where your code run in.
catch is what that catch errors in your code wrote in try block.
Phantom
(Jishnu)
July 20, 2021, 6:45am
#15
Error
Started Compiling Project WebPost
Buildfile: /compiler/android/build.xml
javac:
[mkdir] Created dir: /compiler/android/build/NDVGR/classes
[javac] Compiling 1 source file to /compiler/android/build/NDVGR/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] /compiler/android/src/NDVGR/com/web/post/WebPost.java:45: error: ';' expected
[javac] public void TestFunction()try{
[javac] ^
[javac] /compiler/android/src/NDVGR/com/web/post/WebPost.java:65: error: illegal start of type
[javac] catch(Exception e){
[javac] ^
[javac] 2 errors
[javac] 1 warning
Code
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
public class WebPost extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
public WebPost(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleFunction(description = "b")
public void TestFunction()try{
URL obj = new URL("https://www.google.com");
HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
httpConnection.setDoOutput(true); //if you want output
httpConnection.setRequestMethod("POST"); //request method
BufferedReader bufferedReader = null;
if (httpConnection.getResponseCode() == 200) {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
} else {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
}
StringBuilder content = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
content.append(line).append("\n");
}
bufferedReader.close();
final String con = String.valueOf(content); //post wherever you need
}
catch(Exception e){
e.printStackTrace();
}
}
@Raja_HarshVardhanSolanki i got this error
iamwsumit
(Sumit Kumar)
July 20, 2021, 6:50am
#16
Phantom:
TestFunction()
You missed a braces here. It should be public void TestFunction(){
.
Also try and catch is not enough for that. You need to use the thread for it. A example is here for sending a get request to URL.
public void To(String to){
this.to=to;
}
@DesignerProperty(editorType = "String",defaultValue = "USD")
@SimpleProperty(description = "Set the currency type from that you want to get the prize")
public void From(String to){
this.from=to;
}
public class getprize extends Thread {
int am=amount;
private void get() {
String status="Getting data";
}
public void run() {
try {
URL url=new URL(CurrencyConverter.url);
HttpURLConnection h= (HttpURLConnection) url.openConnection();
Phantom:
try{
try function only run in a method.
try this:
public void TestFunction(){
try{***}
catch{***}
}
Phantom
(Jishnu)
July 20, 2021, 7:07am
#18
how to show the data
Data(httpConnection.getInputStream());
full code
import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@DesignerComponent(
version = 1,
description = "",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "")
@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")
public class WebPost extends AndroidNonvisibleComponent {
//Activity and Context
private Context context;
private Activity activity;
public WebPost(ComponentContainer container){
super(container.$form());
this.activity = container.$context();
this.context = container.$context();
}
@SimpleFunction(description = "b")
public void run() {
try {
URL obj = new URL("https://www.google.com");
HttpURLConnection httpConnection = (HttpURLConnection)obj.openConnection();
httpConnection.setDoOutput(true); //if you want output
httpConnection.setRequestMethod("POST"); //request method
BufferedReader bufferedReader = null;
if (httpConnection.getResponseCode() == 200) {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
Data(httpConnection.getInputStream());
} else {
bufferedReader = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
}
StringBuilder content = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null){
content.append(line).append("\n");
}
bufferedReader.close();
final String con = String.valueOf(content); //post wherever you need
}
catch(Exception e){
e.printStackTrace();
}
}
}
Phantom
(Jishnu)
July 20, 2021, 7:12am
#19
This is the error
Started Compiling Project WebPost
Buildfile: /compiler/androidX/build.xml
javac:
[mkdir] Created dir: /compiler/androidX/build/Qlqhp/classes
[javac] Compiling 1 source file to /compiler/androidX/build/Qlqhp/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 1.7
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/simple_components.json
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/simple_components.txt
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/simple_components_build_info.json
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/AutogeneratedOdeMessages.java
[javac] Note: Wrote file file:/compiler/androidX/build/Qlqhp/classes/ComponentsTranslation.java
[javac] /compiler/androidX/src/Qlqhp/com/web/post/WebPost.java:54: error: cannot find symbol
[javac] Data(httpConnection.getInputStream());
[javac] ^
[javac] symbol: method Data(InputStream)
[javac] location: class WebPost
[javac] 1 error
@Raja_HarshVardhanSolanki @iamwsumit ???
what is the mistake
iamwsumit
(Sumit Kumar)
July 20, 2021, 7:27am
#20
Phantom:
method Data
Have you made Data event in your code? If not then you need to make it.
One more thing you should to do to get the data
Use
Data(bufferedReader.readLine()) ;
Instead of passing input stream to the param.
From where you call this Data function.