mardi 4 août 2015

Android: HttpURLConnection redirects - probably cached

I have problem with HTTP calling, when I have multiple redirects and multiple calls. Lets have following code:

con = (HttpURLConnection) (new
URL("http://server/?function=auth/fetch_internal_ip")).openConnection();
HttpURLConnection.setFollowRedirects(false);
con.setRequestMethod("GET");
con.setDoInput(true);
con.setDoOutput(true);
con.setConnectTimeout(1000);
con.setReadTimeout(1000);
con.connect(); StringBuffer buffer = new StringBuffer();
is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null)
buffer.append(line).append("\r\n");
is.close();
con.disconnect();
is = null;
ip = buffer.toString().trim();

For first using works perfectly, but for any other in the future are not redirects followed. Only reinstall of app or restart phone helps.

Where could be a problem?



via Chebli Mohamed

Android Studio doesn't see Android processes

I apologize for my English.
I can't attach debugger to process, which I run on my device.
I'm selecting Run->Attach debugger to Android process. Then I check Show all processes in Choose process window, but the processes are not there. My device is identified correctly.

I'm also checking DDMS->Devices in Android Device Monitor. Processes are not there, although my device is identified correctly, and pie Chart on System Information tab display "CPU load" information about all device's processes.

I'm trying:

  • Restarting my Mac;
  • Restarting my device (I have Meizu M2 Note, Android 5.1(API 22);
  • Reenabling Settings->Developer Options->USB debugging on the device
  • Restarting adb by running adb kill-server and adb start-server

I use Android Studio 1.2.2.
I read the similar post Can't attach Android Studio's debugger to Android process, but my post a little about other. I can't see processes even in DDMS.

General problem is the following: I can't test In-app Purchasing. If I test it in debug-mode on my Mac, Google Play does not make a purchase. But if I install application as alpha-tester from Google Play, and run it on device, debugger can't see this application.
What I'm doing wrong?



via Chebli Mohamed

Voice recorder application not working

I created a voice recorder application I don't know why it crashes when I click record button.

public class MainActivity extends AppCompatActivity {

private static final String LOG_TAG = "Audio_recorder_basic";
private MediaRecorder recorder = null;
private String path = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void startRec(View view){
    String status = Environment.getExternalStorageState();
    if(status.equals("mounted")){
        path = Environment.getExternalStorageDirectory()+"/Seba/audiorecordtest.3gp";

        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        recorder.setOutputFile(path);
        try {
            recorder.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(LOG_TAG, "prepare() failed");

        }
        recorder.start();
    }
    else{
        Toast.makeText(getApplicationContext(), "SD not mounted", Toast.LENGTH_LONG).show();
        Log.e(LOG_TAG, "SD not mounted");
    }


}
public void stopRec(View view){
    recorder.stop();
    recorder.release();
}

The app only has 2 buttons with android:onClick="startRec" android:onClick="stopRec"

It also has 3 permissions (write and record audio).

<manifest xmlns:android="http://ift.tt/nIICcg"
package="com.example.audiorecbas.audio_recorder_basic" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

path variable extracted from logcat path = /storage/emulated/0/Seba/audiorecordtest.3gp

This is part of the message from Logcat:

    08-04 13:26:57.923    4216-4216/com.example.audiorecbas.audio_recorder_basic I/ViewRootImpl﹕ ViewRoot's Touch Event : ACTION_UP
08-04 13:26:57.941    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ java.io.IOException: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.943    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.File.createNewFile(File.java:946)
08-04 13:26:57.943    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.example.audiorecbas.audio_recorder_basic.MainActivity.startRec(MainActivity.java:37)
08-04 13:26:57.943    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.943    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.944    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$1.onClick(View.java:3829)
08-04 13:26:57.944    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View.performClick(View.java:4461)
08-04 13:26:57.944    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$PerformClick.run(View.java:18526)
08-04 13:26:57.944    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
08-04 13:26:57.945    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 13:26:57.945    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
08-04 13:26:57.945    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 13:26:57.945    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.945    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.945    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-04 13:26:57.947    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-04 13:26:57.947    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
08-04 13:26:57.947    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.948    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-04 13:26:57.948    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-04 13:26:57.949    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.File.createNewFile(File.java:939)
08-04 13:26:57.949    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ ... 15 more
08-04 13:26:57.949    4216-4216/com.example.audiorecbas.audio_recorder_basic E/Audio_recorder_basic﹕ create new file failed
08-04 13:26:57.949    4216-4216/com.example.audiorecbas.audio_recorder_basic E/Audio_recorder_basic﹕ /storage/emulated/0/Seba/audiorecordtest.3gp
08-04 13:26:57.977    4216-4216/com.example.audiorecbas.audio_recorder_basic V/MediaProfiles﹕ getInstance
08-04 13:26:57.977    4216-4216/com.example.audiorecbas.audio_recorder_basic V/MediaProfiles﹕ getLgeCamcorderCapParamByName: LgeCamcorderCap.audiozoomenable
08-04 13:26:57.977    4216-4216/com.example.audiorecbas.audio_recorder_basic E/MediaProfiles﹕ The mLgeCamcorderCap is not created, then return -1
08-04 13:26:57.977    4216-4216/com.example.audiorecbas.audio_recorder_basic D/MediaRecorder﹕ mAudioZoomEnable = -1
08-04 13:26:57.981    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ java.io.FileNotFoundException: /storage/emulated/0/Seba/audiorecordtest.3gp: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.984    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:458)
08-04 13:26:57.985    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
08-04 13:26:57.985    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
08-04 13:26:57.985    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
08-04 13:26:57.985    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.media.MediaRecorder.prepare(MediaRecorder.java:768)
08-04 13:26:57.985    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.example.audiorecbas.audio_recorder_basic.MainActivity.startRec(MainActivity.java:51)
08-04 13:26:57.985    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.986    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.986    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$1.onClick(View.java:3829)
08-04 13:26:57.986    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View.performClick(View.java:4461)
08-04 13:26:57.986    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.view.View$PerformClick.run(View.java:18526)
08-04 13:26:57.986    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
08-04 13:26:57.987    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
08-04 13:26:57.987    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
08-04 13:26:57.987    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 13:26:57.987    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-04 13:26:57.987    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-04 13:26:57.987    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-04 13:26:57.988    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-04 13:26:57.988    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
08-04 13:26:57.988    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOTDIR (Not a directory)
08-04 13:26:57.989    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.Posix.open(Native Method)
08-04 13:26:57.989    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-04 13:26:57.990    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:442)
08-04 13:26:57.990    4216-4216/com.example.audiorecbas.audio_recorder_basic W/System.err﹕ ... 19 more
08-04 13:26:57.990    4216-4216/com.example.audiorecbas.audio_recorder_basic E/Audio_recorder_basic﹕ prepare() failed
08-04 13:26:57.997    4216-4216/com.example.audiorecbas.audio_recorder_basic E/MediaRecorder﹕ start called in an invalid state: 4
08-04 13:26:58.001    4216-4216/com.example.audiorecbas.audio_recorder_basic D/AndroidRuntime﹕ Shutting down VM
08-04 13:26:58.001    4216-4216/com.example.audiorecbas.audio_recorder_basic W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d41d58)
08-04 13:26:58.004    4216-4216/com.example.audiorecbas.audio_recorder_basic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.audiorecbas.audio_recorder_basic, PID: 4216
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3834)

I use LG L50 for development.



via Chebli Mohamed

Date not printing correct Time Android

I want to print the Time (hours:minutes:seconds) from a date gotten from a String.

I have a string with the next value:

String dateStr = "Tue, 04 Aug 2015 12:09:10 GMT"

I parse it to a Date:

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
Date d = format.parse(dateStr);

But when I try to get the hours, minutes and seconds, I get a different value:

System.out.println("The current time is: " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds());

Prints: "The current time is: 9:9:10"

Whats wrong? Shouldn't print "The current time is: 12:9:10"?

Thanks :)


More information:

When I create the Date, it automatically sets the Timezone to GMT-03:00 2015, and that's why is printing 3 hours less. How can I set the timezone?



via Chebli Mohamed

Create PreferenceDialog onClick on filter popup?

My goal is to implement the image below.

How to create a PreferenceDialog if the filter icon in my toolbar has been clicked?

I want to set some preferences to filter the entries of my list.

I'm at this point. Is that the right way to implement this?

enter image description here



via Chebli Mohamed

Cordova Android opening in browser

I have an application that I built using Cordova. I'm running into an issue where my login page opens correctly in the app but then when they click "login" the next page opens in Chrome on an Android device. I tested this on an iOS device as well and it works correctly: The second page opens in the app, not Safari. I couldn't find any threads that solve this issue so I was wondering if I'm missing something.

The code I'm using for the login is:

window.location = returnUrl;

Where returnUrl = the new url if the login is successful.

I also tried window.open(returnUrl, '_self'); which didn't work either.

Any help would be appreciated!



via Chebli Mohamed

Android LinearLayout items overlap each other when one changes visibillity properties

I have a view with a LinearLayout which contains a bunch of items to show. In initialization code I add these items into layout.

for(int i = 0; i < n; i++) {
    View view = new SwitchButton(mTexts.get(i));
    mLinearLayout.add(view);
}

On top of this LinearLayout I have a TabLayout with several tabs. Whenever a user switches to another tab I set visibility property to each item in the layout according to to a specific tab.

@Override
public void onTabSelected(Tab tab) {
    if (tab.getPosition() != mLastSelectedTab) {
        for (int i = 0; i < mLinearLayout.getChildCount(); i++) {
            View item = mLinearLayout.getChildAt(i);
            if (isVisibleInTab(tab.getPosition(), i) {
                 item.setVisibility(View.VISIBLE);
            } else {
                 item.setVisibility(View.GONE);
            }
        }
    }
}

But when a user changes tab for a short period of time linear layout items overlap one another. Even if I set

android:animateLayoutChanges="true"

in my layout, overlapping still occurs.



via Chebli Mohamed