Commit cccf806c authored by Pyone Thant Win's avatar Pyone Thant Win
Browse files

Andriod API call update

parent a671e044
......@@ -15,10 +15,11 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".foodInfo"></activity>
<activity
android:name=".take_picture"
android:label="@string/title_activity_take_picture"
android:theme="@style/AppTheme.NoActionBar"></activity>
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".login" />
<activity android:name=".MainActivity">
<intent-filter>
......
package com.example.calorieestimator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Arrays;
public class foodInfo extends AppCompatActivity {
TextView _api_result;
EditText _food_name;
Button _btn_calc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_info);
_api_result = (TextView)findViewById(R.id.api_view);
_food_name = (EditText)findViewById(R.id.food_name);
_btn_calc = (Button)findViewById(R.id.btn_calc);
_btn_calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String food = _food_name.getText().toString();
String[] t = food.split(" ", 0);
String api_helper = "";
int len = t.length;
for (int i=0; i<len; i++){
api_helper = api_helper + t[i]+ "%20";
}
api_helper = api_helper + "Raw";
//Log.d("Result: ", api_helper);
String URL1 = "https://api.nal.usda.gov/fdc/v1/foods/search?api_key=KEVJZOV5r0tWduJkb2efmIX5hsp78tjCggR4jdBY&query=";
String URL = URL1 + api_helper;
RequestQueue requestQueue = Volley.newRequestQueue(foodInfo.this);
JsonObjectRequest objectRequest = new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray jsonArray = response.getJSONArray("foods");
JSONObject food = jsonArray.getJSONObject(0);
String description = food.getString("description");
JSONArray foodNutrients = food.getJSONArray("foodNutrients");
JSONObject nutrients = foodNutrients.getJSONObject(3);
String amount = nutrients.getString("nutrientNumber");
String kcal = nutrients.getString("unitName");
String final_str = description + ": " + amount + kcal;
_api_result.setText(final_str.toString());
//Log.e("Rest Response", response.toString());
} catch (JSONException e){
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error Response", error.toString());
}
}
);
requestQueue.add(objectRequest);
}
});
}
}
......@@ -20,35 +20,21 @@ public class take_picture extends AppCompatActivity {
//Button _btn_cam;
private ImageView _imageView;
private static final int REQUEST_IMAGE_CAPTURE = 101;
Button _btn_next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_picture);
_imageView = findViewById(R.id.imageView);
//_btn_cam = (Button)findViewById(R.id.btn_cam);
//Intent imageTakeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//if (imageTakeIntent.resolveActivity(getPackageManager())!=null){
// startActivityForResult(imageTakeIntent, REQUEST_IMAGE_CAPTURE);
/*_btnregister.setOnClickListener(new View.OnClickListener(){
_btn_next = (Button)findViewById(R.id.btn_next);
_btn_next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
db = openHelper.getWritableDatabase();
String fname = _fname.getText().toString();
String lname = _lname.getText().toString();
String email = _user_email.getText().toString();
String pswd = _user_pswd.getText().toString();
insertdata(fname, lname, email, pswd);
Toast.makeText(getApplicationContext(), "register successfully", Toast.LENGTH_LONG).show();
public void onClick(View view) {
Intent intent = new Intent(take_picture.this, foodInfo.class);
startActivity(intent);
}
});*/
});
}
public void btn_cam(View view) {
......@@ -59,6 +45,7 @@ public class take_picture extends AppCompatActivity {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
Bundle extras = data.getExtras();
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".foodInfo">
<EditText
android:id="@+id/food_name"
android:layout_width="203dp"
android:layout_height="49dp"
android:layout_marginStart="102dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="96dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="262dp"
android:layout_height="41dp"
android:layout_marginStart="150dp"
android:layout_marginTop="151dp"
android:layout_marginEnd="150dp"
android:layout_marginBottom="34dp"
android:text="What is the name of the food?"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/food_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/api_view"
android:layout_width="253dp"
android:layout_height="284dp"
android:layout_marginStart="83dp"
android:layout_marginTop="115dp"
android:layout_marginEnd="75dp"
android:layout_marginBottom="71dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/food_name" />
<Button
android:id="@+id/btn_calc"
android:layout_width="143dp"
android:layout_height="37dp"
android:layout_marginTop="26dp"
android:text="Get Calories"
app:layout_constraintBottom_toTopOf="@+id/api_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/food_name"
app:layout_constraintVertical_bias="0.512" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
......@@ -12,28 +12,44 @@
android:layout_width="126dp"
android:layout_height="55dp"
android:layout_marginStart="142dp"
android:layout_marginTop="550dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="143dp"
android:layout_marginBottom="126dp"
android:layout_marginBottom="50dp"
android:onClick="btn_cam"
android:text="Open Camera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btn_next"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_width="303dp"
android:layout_height="317dp"
android:layout_marginStart="80dp"
android:layout_marginTop="187dp"
android:layout_marginTop="170dp"
android:layout_marginEnd="81dp"
android:layout_marginBottom="113dp"
android:layout_marginBottom="50dp"
app:layout_constraintBottom_toTopOf="@+id/btn_cam"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@mipmap/ic_launcher" />
<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="162dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="162dp"
android:layout_marginBottom="50dp"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_cam" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment