connect flutter app to localhost

484 views
Skip to first unread message

abha nagpure

unread,
Feb 11, 2020, 6:39:25 AM2/11/20
to Flutter Development (flutter-dev)
i want to convert this java code for android app to flutter (dart) to connect localhost



public class HttpHandler {

private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler(){
}
public String makeServiceCall(String keyword,String para,String data) {
Log.d("enter url","enter in httpclass");

String reqUrl1="http://192.168.1.16:8082/gotrade_merchant/callservice?keyword="+keyword+ "&para=" +para+ "&data=" +data+"";
// String reqUrl1 = "http://192.168.1.10//gotrademob/gosellsy/goTrade/fetch?keyword="+keyword+ "&para=" +para+ "&data=" +data+"";
// String reqUrl1 = "http://192.168.1.18:8080//gotrademob/gosellsy/goTrade/fetch?keyword="+keyword+ "&para=" +para+ "&data=" +data+"";
String reqUrl = reqUrl1.replace(" ", "%20").replace("#", "");
Log.d("reqUrl>>",""+reqUrl);
String response="null";
try{
URL url=new URL(reqUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Language", "en-US");
conn.setUseCaches (false);
conn.setDoInput(true);
conn.setDoOutput(true);
// DataOutputStream wr = new DataOutputStream (conn.getOutputStream ());
// wr.writeBytes (reqUrl1);
// wr.flush ();
// wr.close ();
InputStream in= new BufferedInputStream(conn.getInputStream());
Log.d("in",""+in);
response = convertStreamToString(in);
}
catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {

Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
}
catch (Exception e){
Log.e(TAG, "Exception: " + e.getMessage());
}
Log.d("response>>>",""+response);
return response;
}
private String convertStreamToString(InputStream is){

BufferedReader reader= new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
String line;
try{
while((line= reader.readLine())!=null){
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try{
is.close();
reader.close();

} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}

abha nagpure

unread,
Feb 11, 2020, 6:41:57 AM2/11/20
to Flutter Development (flutter-dev)


On Tuesday, February 11, 2020 at 5:09:25 PM UTC+5:30, abha nagpure wrote:
i want to convert this java code for android app to flutter (dart) to connect localhost
i tried so hard but i am not getting please help me out 

tomohiro suzuki

unread,
Feb 11, 2020, 8:47:05 AM2/11/20
to abha nagpure, Flutter Development (flutter-dev)
Which part could you not write in Dart/Flutter?
(Or share what you converted in Dart/Flutter so far)

Sent from my iPad

On Feb 11, 2020, at 6:42 AM, abha nagpure <abhana...@gmail.com> wrote:


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/bbce0e16-5bf2-4f17-8a48-4abc5af0115b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages