update get api
This commit is contained in:
parent
63e6a5b7dd
commit
a494c02d90
|
@ -61,6 +61,49 @@ public class HttpCallHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public String executeGetBalance(String urlParam){
|
||||
BufferedReader bufferedReader = null;
|
||||
HttpURLConnection connection = null;
|
||||
String res = null;
|
||||
try{
|
||||
URL url = new URL(urlParam);
|
||||
connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.connect();
|
||||
|
||||
int statusCode = connection.getResponseCode();
|
||||
|
||||
if(statusCode != 200){
|
||||
return null;
|
||||
}else {
|
||||
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine())!=null){
|
||||
stringBuilder.append(line);
|
||||
}
|
||||
JSONObject jsonSuccess = new JSONObject(stringBuilder.toString());
|
||||
jsonSuccess.put("status",200);
|
||||
res = jsonSuccess.toString();
|
||||
}
|
||||
return res;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} finally {
|
||||
if(connection!=null){
|
||||
connection.disconnect();
|
||||
}
|
||||
if(bufferedReader != null){
|
||||
try{
|
||||
bufferedReader.close();
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String executeLoginHttp(String urlParam, String[] params){
|
||||
BufferedReader bufferedReader = null;
|
||||
HttpURLConnection connection = null;
|
||||
|
|
|
@ -11,6 +11,8 @@ import android.widget.Toast;
|
|||
|
||||
import com.example.monnthic.monnethicmobile.R;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
@ -19,13 +21,14 @@ import java.net.URL;
|
|||
|
||||
import monnethic.mobile.database.User;
|
||||
import monnethic.mobile.restApi.Config;
|
||||
import monnethic.mobile.restApi.HttpCallHandler;
|
||||
import monnethic.mobile.transaction.MakePayementActivity;
|
||||
import monnethic.mobile.transaction.ReceivePayementActivity;
|
||||
|
||||
public class UserAccountActivity extends AppCompatActivity {
|
||||
|
||||
private TextView balance;
|
||||
private String strUrl = Config.QUERY_BALANCE+"b";
|
||||
private String strUrl = Config.QUERY_BALANCE;
|
||||
private String result;
|
||||
|
||||
@Override
|
||||
|
@ -68,7 +71,8 @@ public class UserAccountActivity extends AppCompatActivity {
|
|||
}
|
||||
});
|
||||
|
||||
new GetUserBalanceTask().execute(strUrl);
|
||||
String[] params = {strUrl,"b"};
|
||||
new GetUserBalanceTask().execute(params);
|
||||
}
|
||||
|
||||
public void sendPayement(){
|
||||
|
@ -107,46 +111,16 @@ public class UserAccountActivity extends AppCompatActivity {
|
|||
|
||||
@Override
|
||||
protected String doInBackground(String... params) {
|
||||
System.out.println(params[0]);
|
||||
InputStream inputStream = null;
|
||||
HttpURLConnection conn = null;
|
||||
try{
|
||||
URL url = new URL(params[0]);
|
||||
conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.connect();
|
||||
|
||||
int response = conn.getResponseCode();
|
||||
if(response != 200){
|
||||
return "Error response";
|
||||
}
|
||||
|
||||
inputStream = conn.getInputStream();
|
||||
if(inputStream == null){
|
||||
return "Error inputStream";
|
||||
}
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
String line;
|
||||
|
||||
while ((line = reader.readLine())!=null){
|
||||
buffer.append(line);
|
||||
}
|
||||
|
||||
String value = new String(buffer);
|
||||
|
||||
System.out.println("result is "+value);
|
||||
result = value;
|
||||
|
||||
String url = Config.QUERY_BALANCE+params[0];
|
||||
HttpCallHandler httpCallHandler = new HttpCallHandler();
|
||||
JSONObject json = new JSONObject(httpCallHandler.executeGetBalance(url));
|
||||
Double balance = json.getDouble("userBalance");
|
||||
return balance.toString();
|
||||
} catch (Exception e){
|
||||
System.out.println("Exception : "+e);
|
||||
} finally {
|
||||
if(conn != null){
|
||||
conn.disconnect();
|
||||
}
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue